Added performance explanation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent d750a37 · Browse files · 5 files·+38 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+38 added−33 removed
- other:+38 added−33 removed
Provenance
- Commit
1fea836- Tree
bfc4aa3- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
d750a37
5 files changed+38 added−33 removed
modified:__performance_tests__/todo.js+18 added-5 removed
40 unmodified lines4142434445464546474849474849505152535455565758596061626364656667114 unmodified linesExpand all40 unmodified lines global.gc && global.gc() test(name, fn) }
{ const draft = cloneDeep(baseState) measure("just mutate", () => { for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { baseState[i].done = true } }) measure("just mutate", () => { for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { draft[i].done = true } }) }
{ const draft = cloneDeep(baseState) measure("just mutate, freeze", () => { for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { draft[i].done = true } deepFreeze(draft) }) }
measure("deepclone, then mutate", () => { const draft = cloneDeep(baseState) for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { draft[i].done = true114 unmodified linesExpand alladded:images/immer.pngbinary
A binary file, added.
added:images/performance.pngbinary
A binary file, added.
deleted:immer.pngbinary
A binary file, deleted.
modified:readme.md+20 added-28 removed
9 unmodified lines10 The basic idea is that you will apply all your changes to a temporarily _draftState_, which is a proxy of the _currentState_.11 Once all your mutations are completed, immer will produce the _nextState_ based on the mutations to the draft state.12 This means that you can interact with your data by simply modifying it, while keeping all the benefits of immutable data.13 14-14+15 16 Using immer is like having a personal assistant; he takes a letter (the current state), and gives you a copy (draft) to jot changes onto. Once you are done, the assistant will take your draft and produce the real immutable, final letter for you (the next state).17 18 A mindful reader might notice that this is quite similar to `withMutations` of ImmutableJS. It is indeed, but generalized and applicable to plain, native JavaScript data structures (arrays and objects) without further needing any library.213 unmodified lines232 })233 }234 ```235 236-## Performance237-238-Here is a [simple benchmark](__tests__/performance.js) on the performance of `immer`.239-This test takes 100.000 todo items, and updates 10.000 of them.240-These tests were executed on Node 8.4.0.241-242-Use `yarn test:perf` to reproduce them locally243-244-```245- ✓ just mutate (3ms)246- (No immutability at all)247- ✓ deepclone, then mutate (409ms)248- (Clone entire tree, then mutate (no structural sharing!))249- ✓ handcrafted reducer (17ms)250- (Implement it as typical Redux reducer, with slices and spread operator)251- ✓ immutableJS (60ms)252- (Use immutableJS and leverage `withMutations` for best performance)253- ✓ immer (proxy) - with autofreeze (305ms)254- (Immer, with auto freeze enabled, default implementation)255- ✓ immer (proxy) - without autofreeze (149ms)256- (Immer, with auto freeze disabled, default implementation)257- ✓ immer (es5) - with autofreeze (436ms)258- (Immer, with auto freeze enabled, compatibility implementation)259- ✓ immer (es5) - without autofreeze (336ms)260- (Immer, with auto freeze disabled, default implementation)261-```262-236 ## Limitations237 238 * Currently, only tree shaped states are supported. Cycles could potentially be supported as well (PR's welcome)239 * Currently, only supports plain objects and arrays. Non-plain data structures (like `Map`, `Set`) not (yet). (PR's welcome)2 unmodified lines242 243 * Make sure to modify the draft state you get passed in in the callback function, not the original current state that was passed as the first argument to `immer`!244 * Since immer uses proxies, reading huge amounts of data from state comes with an overhead. If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the `immer` block or read from the `currentState` rather than the `draftState`245 246+## Performance247+248+Here is a [simple benchmark](__tests__/performance.js) on the performance of `immer`.249+This test takes 100.000 todo items, and updates 10.000 of them.250+_Freeze_ indicates that the state tree has been frozen after producing it. This is a _development_ best practice, as it prevents developers from accidentally modifying the state tree.251+252+These tests were executed on Node 8.4.0.253+Use `yarn test:perf` to reproduce them locally.254+255+256+257+Some observations:258+* The _mutate_, and _deepclone, mutate_ benchmarks establish a baseline on how expensive changing the data is, without immutability (or structural sharing in the deep clone case).259+* The _reducure_ and _naive reducer_ are implemented in typical Redux style reducers. The "smart" implementation slices the collection first, and then maps and freezes only the relevant todos. The "naive" implementation just maps over and processes the entire collection.260+* Immer with proxies is roughly speaking twice as slow as a hand written reducer. This is in practice negclectable.261+* Immer is roughly as fast as ImmutableJS. However, the _immutableJS + toJS_ makes clear the cost that often needs to be paid later; converting the immutableJS objects back to plain objects, to be able to pass them to components, over the network etc... (And there is also the upfront cost of converting data received from e.g. the server to immutable JS)262+* The ES5 implentation of immer is significantly slower. For most reducers this won't matter, but reducers that process large amounts of data might benefit from not (or only partially) using an immer producer. Luckily, immer is fully opt-in.263+* The peeks in the _frozen_ versions of _just mutate_, _deepclone_ and _naive reducer_ come from the fact that they recursively freeze the full state tree, while the other test cases only freeze the modified parts of the tree.264+265 ## Credits266 267 Special thanks goes to @Mendix, which supports it's employees to experiment completely freely two full days a month, which formed the kick-start for this project.