Palindrom / JSONPatcherProxy

ES6 proxy powered JSON Object observer that emits JSON patches when changes occur to your object tree.
94 stars 14 forks source link

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

Closed tomalec closed 5 years ago

tomalec commented 5 years ago

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 commented 5 years ago

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

alshakero commented 5 years ago

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

warpech commented 5 years ago

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

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

warpech commented 5 years ago

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