Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect to proxy object, to observe changes made by setters #41

Closed
wants to merge 2 commits into from

Conversation

tomalec
Copy link
Member

@tomalec tomalec commented Jun 18, 2019

For example, to be able to observe the change made to obj.foo by setting obj.bar='new'

let obj = {
  'foo': 'old'
};
Object.defineProperty(obj, 'bar', {
  set: function (newValue) {
    this.foo = newValue;
  },
  get: function () {
    return this.foo
  },
  enumerable: true
});

Before this PR, this in setter was referring to original object not the proxy.

This change decreases performance significantly. According to npm run bench executed 3 times on my local it's:

jsonpatcherproxy generate operation -20,97%
jsonpatch generate operation -6,31%
jsonpatcherproxy mutation - huge object -5,12%
jsonpatch mutation - huge object -5,21%
jsonpatcherproxy generate operation - huge object -9,18%
jsonpatch generate operation - huge object -1,73%
PROXIFY big object -1,05%
DIRTY-OBSERVE big object -5,67%

@tomalec tomalec requested a review from warpech June 18, 2019 14:29
@tomalec
Copy link
Member Author

tomalec commented Jun 18, 2019

We can argue, that JS object with setter is not a JSON, therefore, we should not compromise performance for non-standard object.

@alshakero
Copy link
Collaborator

React Easy State does not make this sacrifice.

See "Avoid using the this keyword in the methods of your state stores."

https://github.com/solkimicreb/react-easy-state

observedObj = JSONPatcherProxy.deepClone(observedObj);

expect(obj2).toReallyEqual(observedObj);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think lined 200-210 are redundant in this test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well they check if the patch application leads to the correct result. However they are quire redundant with fast-json-patch tests.

@warpech
Copy link
Contributor

warpech commented Jun 24, 2019

It was discussed on a HO between me and @tomalec that we should introduce 2 modes of operation in JSONPatcherProxy:

  • fast that does not reflect the proxy object to get and set operations. In fact, it does not need to use the Reflect API at all, it can use a quicker method: tree[key] = newValue.
  • slow that reflects the proxy object to get and set operations. It must use the Reflect API

I can volunteer to do this change, but only if PR #42 is accepted first. Otherwise I would go nuts with the tests :)

@warpech
Copy link
Contributor

warpech commented Jun 24, 2019

This PR was rebased on current master and is now available as a new PR #44

@warpech warpech closed this Jun 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants