4 ms·
Having experimented with and used proxies a decent amount, the MDN page is my goto reference when I need to use them: https://developer.mozilla.org/en-US/docs/W
by benjaminjackman 8y ago
Having experimented with and used proxies a decent amount, the MDN page is my goto reference when I need to use them: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
The biggest pitfalls, in my experience are
1. Infinite recursion when implementing get and printing proxies with console.log in node.js (which attempts to access properties from the object prototype which can lead to infinite recursion problems).
2. Performance, which I would need to do more digging and benchmarking to comment on in more depth.
Some libraries that use Proxies to great effect are the https://github.com/mobxjs/mobx-state-tree https://github.com/mobxjs/mobx-state-tree and https://github.com/mweststrate/immer https://github.com/mweststrate/immer libraries both by mweststrate
- fro0116 8y agoImmer is excellent. It makes deeply nested immutable updates a breeze. The API almost makes it feel like you're programming in a fantasy-land future where JavaScript's default data structures were persistent data structures like a proper functional language. And it accomplishes all that without sacrificing type safety like most string path based approaches to deep updates often tend to do (think lodash.set or ramda.assocPath). The only reason we haven't gone all in on it yet is the existence of browsers that don't support Proxy (like IE11, which we now unfortunately have to support), where performance takes a huge hit, and those tend to be the browsers that can least afford to take that performance hit to begin with.
- acemarke 8y agoYep, we're using Immer in our new Redux Starter Kit package to help make writing Redux reducers easy: https://redux-starter-kit.js.org https://redux-starter-kit.js.org
- pault 8y agoI just converted one of our medium sized redux apps to immer and it has been a resounding success. I highly recommend it and will be using it everywhere in the future.
- acemarke 8y agoHi, I'm a Redux maintainer. I'd be interested in hearing how your experience with Immer went, as well as any other feedback you can offer. Also, as I mentioned just below, please try out our new Redux Starter Kit package, which uses Immer internally (and offers several other useful utilities for simplifying common Redux use cases, like store setup, reducer definitions, and even creating entire "slices" of state at once): https://redux-starter-kit.js.org https://redux-starter-kit.js.org
- pault 8y agoActually the reason I was willing to take a chance on Immer is because it's included in redux starter kit. :) Using immer in production has been about 1000x better than writing immutable state updates by hand. It literally reduced (hah) the LOC in our reducers by at least 75%, not to mention the gains from reduced cognitive overhead. The architecture I ended up using is basically a recreation of redux starter kit with some customization for convenience (a couple of methods on action creators that wrap `toString` for creating reducer keys that only handle actions with certain meta properties, like `updateEntities.of(entity)`).
- acemarke 8y agoNeat! I'd be interested in seeing what you came up with if you get a chance.
- chrisparton1991 8y agoI rewrote my reducers with Immer as well. I had some pretty complex nested updates that became super simple with Immer, love it!