Skip to content
Myr

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

5 files+38 added−33 removed

modified:__performance_tests__/todo.js+18 added-5 removed
added: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 lines
10The basic idea is that you will apply all your changes to a temporarily _draftState_, which is a proxy of the _currentState_.11Once all your mutations are completed, immer will produce the _nextState_ based on the mutations to the draft state.12This means that you can interact with your data by simply modifying it, while keeping all the benefits of immutable data.1314![immer.png](immer.png)14![immer.png](images/immer.png)1516Using 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).1718A 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 lines
232 })233}234```235236## Performance237238Here is a [simple benchmark](__tests__/performance.js) on the performance of `immer`.239This test takes 100.000 todo items, and updates 10.000 of them.240These tests were executed on Node 8.4.0.241242Use `yarn test:perf` to reproduce them locally243244```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```262236## Limitations237238* 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 lines
242243* 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`245246## Performance247248Here is a [simple benchmark](__tests__/performance.js) on the performance of `immer`.249This 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.251252These tests were executed on Node 8.4.0.253Use `yarn test:perf` to reproduce them locally.254255![immer.png](images/immer.png)256257Some 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.264265## Credits266267Special 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.