Skip to content
Myr

Update readme

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 2513fb8 · Browse files · 5 files+54 added−11 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+54 added−11 removed

  • other:+54 added−11 removed

5 files+54 added−11 removed

modified:__performance_tests__/add-data.js+1 added-1 removed
modified:__performance_tests__/incremental.js+2 added
2 unmodified lines
3import produce, {setAutoFreeze, setUseProxies} from "../dist/immer.umd.js"4import cloneDeep from "lodash.clonedeep"5import * as Immutable from "immutable"67console.log("\n# incremental - lot of small incremental changes\n")89function createTestObject() {10 return {11 a: 1,12 b: "Some data here"
123 unmodified lines
modified:__performance_tests__/todo.js+39 added-1 removed
40 unmodified lines
4142// generate seamless-immutable base state43seamlessBaseState = Seamless.from(baseState)4445console.log("# todo - performance")45console.log("\n# todo - performance\n")4647measure(48 "just mutate",49 () => ({draft: cloneDeep(baseState)}),
162 unmodified lines
212 }213)214215measure(216 "immer (proxy) - with autofreeze - with patch listener",217 () => {218 setUseProxies(true)219 setAutoFreeze(true)220 },221 () => {222 produce(223 baseState,224 draft => {225 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {226 draft[i].done = true227 }228 },229 function() {}230 )231 }232)233234measure(235 "immer (es5) - without autofreeze",236 () => {237 setUseProxies(false)238 setAutoFreeze(false)
39 unmodified lines
278 function() {}279 )280 }281)282283measure(284 "immer (es5) - with autofreeze - with patch listener",285 () => {286 setUseProxies(false)287 setAutoFreeze(true)288 },289 () => {290 produce(291 baseState,292 draft => {293 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {294 draft[i].done = true295 }296 },297 function() {}298 )299 }300)
modified:images/performance.pngbinary

A binary file, modified.

modified:readme.md+12 added-9 removed
222 unmodified lines
223## Patches224225During the run of a producer, Immer can record all the patches that would replay the changes made by the reducer.226This is a very powerful tool if you want to fork your state temporarily, and replay the changes to the original.227228Patches are useful in few scenarios:229 * To exchange incremental updates with other parties, for example over websockets230 * For debugging / traces, to see precisely how state is changed over time231 * As basis for undo/redo or as approach to replay changes on a slightly different state tree232233To help with replaying patches, `applyPatches` comes in handy. Here is an example how patches could be used234to record the incremental updates and (inverse) apply them:235236```javascript
256 unmodified lines
493```494495## Performance496491Here is a [simple benchmark](__performance_tests__/todo.js) on the performance of Immer. This test takes 100.000 todo items, and updates 10.000 of them. _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.497Here is a [simple benchmark](__performance_tests__/todo.js) on the performance of Immer. This test takes 50,000 todo items, and updates 5,000 of them. _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.498493These tests were executed on Node 8.4.0. Use `yarn test:perf` to reproduce them locally.499These tests were executed on Node 9.3.0. Use `yarn test:perf` to reproduce them locally.500501![performance.png](images/performance.png)502497Some observations:503Most important observation:504499* From `immer` perspective, this benchmark is a _worst case_ scenario, because the root collection it has to proxy is really large relatively to the rest of the data set.500* 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).501* The _reducer_ 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.502* Immer with proxies is roughly speaking twice as slow as a hand written reducer. This is in practice negligible.505* Immer with proxies is roughly speaking twice to three times slower as a hand written reducer (the above test case is worst case, se `yarn test:perf` for more tests). This is in practice negligible.506* 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)504* The ES5 implementation 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.505* The peaks 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.507* Generating patches doesn't significantly slow immer down508* The ES5 fallback implementation is roughly twice as slow as the proxy implementation, in some cases worse.509510## FAQ511512_(for those who skimmed the above instead of actually reading)_
36 unmodified lines