performance.md
This file
Path
docs/performance.md
59 lines · 4.7 KiB · Markdown
Last changed by
docs: Organize performance and pitfalls, and document nested produce behavior. Fixes #694Michel Weststratebefore adoption0730231
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–5why, one step back1---2id: performance3title: Immer performance4---5docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 6–8why, one step back6<center>7<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>8</center>Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
line 9why, one step back9Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 10–13why, one step back10<details>11 <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 5: Leveraging Immer's structural sharing in React</summary>12 <br>13 <div style="padding:5px;">docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 14why, one step back14 <iframe style="border: none;" width=760 height=427 scrolling="no" src="https://egghead.io/lessons/react-profile-react-rendering-and-optimize-with-memo-to-leverage-structural-sharing/embed" ></iframe>Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 15–22why, one step back15 </div>16 <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-profile-react-rendering-and-optimize-with-memo-to-leverage-structural-sharing">Hosted on egghead.io</a>17</details>1819<details>20 <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 7: Immer will try to re-cycle data if there was no semantic change</summary>21 <br>22 <div style="padding:5px;">docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 23why, one step back23 <iframe style="border: none;" width=760 height=427 scrolling="no" src="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer/embed" ></iframe>Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 24–27why, one step back24 </div>25 <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer">Hosted on egghead.io</a>26</details>27Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 28–31why, one step back28Here is a [simple benchmark](https://github.com/immerjs/immer/blob/master/__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.2930Something that isn't reflected in the numbers above, but in reality, Immer is sometimes significantly _faster_ than a hand written reducer. The reason for that is that Immer will detect "no-op" state changes, and return the original state if nothing actually changed, which can avoid a lot of re-renderings for example. Cases are known where simply applying immer solved critical performance issues.31feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 32why, one step back32These tests were executed on Node 10.16.3. Use `yarn test:perf` to reproduce them locally.Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 33–39why, one step back33343536Most important observation:3738- Immer with proxies is roughly speaking twice to three times slower as a handwritten reducer (the above test case is worst case, see `yarn test:perf` for more tests). This is in practice negligible.39- 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)Merge pull request #443 from Mudassar045/fix-grammatical-typosMichel Weststratebefore adoption0349152
line 40why, one step back40- Generating patches doesn't significantly slow down immerMerge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
line 41why, one step back41- The ES5 fallback implementation is roughly twice as slow as the proxy implementation, in some cases worse.Immer 7Michel Weststratebefore adoption467ea5d
lines 42–44why, one step back4243## Performance tips44docs: Organize performance and pitfalls, and document nested produce behavior. Fixes #694Michel Weststratebefore adoption0730231
lines 45–59why, one step back45### Pre-freeze data4647When adding a large data set to the state tree in an Immer producer (for example data received from a JSON endpoint), it is worth to call `Object.freeze(json)` on the root of the data to be added first. This will allow Immer to add the new data to the tree faster, as it will skip freezing it, or searching the tree for any changes (drafts) that might be made.4849### You can always opt-out5051Realize that immer is opt-in everywhere, so it is perfectly fine to manually write super performance critical reducers, and use immer for all the normal ones. Even from within a producer you opt-out from Immer for certain parts of your logic by using utilies `original` or `current` and perform some of your operations on plain JavaScript objects.5253### For expensive search operations, read from the original state, not the draft5455Immer will convert anything you read in a draft recursively into a draft as well. If you have expensive side effect free operations on a draft that involves a lot of reading, for example finding an index using `find(Index)` in a very large array, you can speed this up by first doing the search, and only call the `produce` function once you know the index. Thereby preventing Immer to turn everything that was searched for in a draft. Or, alternatively, perform the search on the original value of a draft, by using `original(someDraft)`, which boils to the same thing.5657### Pull produce as far up as possible5859Always try to pull produce 'up', for example `for (let x of y) produce(base, d => d.push(x))` is exponentially slower than `produce(base, d => { for (let x of y) d.push(x)})`