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
Provenance
- Commit
341cfeb- Tree
de1b667- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
2513fb8
5 files changed+54 added−11 removed
modified:__performance_tests__/add-data.js+1 added-1 removed
5 unmodified lines678910101112131489 unmodified lines5 unmodified linesimport {fromJS} from "immutable"import Seamless from "seamless-immutable"import deepFreeze from "deep-freeze"
console.log("# add-data - loading large set of data")console.log("\n# add-data - loading large set of data\n")
const dataSet = require("./data.json")const baseState = { data: null89 unmodified linesmodified:__performance_tests__/incremental.js+2 added
2 unmodified lines3 import produce, {setAutoFreeze, setUseProxies} from "../dist/immer.umd.js"4 import cloneDeep from "lodash.clonedeep"5 import * as Immutable from "immutable"6 7+console.log("\n# incremental - lot of small incremental changes\n")8+9 function createTestObject() {10 return {11 a: 1,12 b: "Some data here"123 unmodified linesmodified:__performance_tests__/todo.js+39 added-1 removed
40 unmodified lines41 42 // generate seamless-immutable base state43 seamlessBaseState = Seamless.from(baseState)44 45-console.log("# todo - performance")45+console.log("\n# todo - performance\n")46 47 measure(48 "just mutate",49 () => ({draft: cloneDeep(baseState)}),162 unmodified lines212 }213 )214 215 measure(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+)233+234+measure(235 "immer (es5) - without autofreeze",236 () => {237 setUseProxies(false)238 setAutoFreeze(false)39 unmodified lines278 function() {}279 )280 }281 )282+283+measure(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 lines223 ## Patches224 225 During the run of a producer, Immer can record all the patches that would replay the changes made by the reducer.226 This is a very powerful tool if you want to fork your state temporarily, and replay the changes to the original.227+228+Patches 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 tree232+233 To help with replaying patches, `applyPatches` comes in handy. Here is an example how patches could be used234 to record the incremental updates and (inverse) apply them:235 236 ```javascript256 unmodified lines493 ```494 495 ## Performance496 491-Here 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.497+Here 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.498 493-These tests were executed on Node 8.4.0. Use `yarn test:perf` to reproduce them locally.499+These tests were executed on Node 9.3.0. Use `yarn test:perf` to reproduce them locally.500 501 502 497-Some observations:503+Most important observation:504 499-* 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.509 510 ## FAQ511 512 _(for those who skimmed the above instead of actually reading)_36 unmodified lines