Skip to content
Myr

performance.mdx

This file

Path

website/docs/performance.mdx

110 lines · 5.6 KiB

Last changed by

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

History of this file

Take it

The walk stopped after 200 commits: lines older than 07f5cc9 point at it.

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 1–11why, one step back
1---
2id: performance3title: Immer performance4---56<center>7 <div8 data-ea-publisher="immerjs"9 data-ea-type="image"10 className="horizontal bordered"11 ></div>

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 12–13why, one step back
12</center>
13<details>

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 14–31why, one step back
14 <summary className="egghead-summary">
15 egghead.io lesson 5: Leveraging Immer's structural sharing in React16 </summary>17 <br />18 <div>19 <iframe20 width="760"21 height="427"22 scrolling="no"23 src="https://egghead.io/lessons/react-profile-react-rendering-and-optimize-with-memo-to-leverage-structural-sharing/embed"24 ></iframe>25 </div>26 <a27 className="egghead-link"28 href="https://egghead.io/lessons/react-profile-react-rendering-and-optimize-with-memo-to-leverage-structural-sharing"29 >30 Hosted on egghead.io31 </a>

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 32–33why, one step back
32</details>
33<details>

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 34–54why, one step back
34 <summary className="egghead-summary">
35 egghead.io lesson 7: Immer will try to re-cycle data if there was no36 semantic change37 </summary>38 <br />39 <div>40 <iframe41 width="760"42 height="427"43 scrolling="no"44 src="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer/embed"45 ></iframe>46 </div>47 <a48 className="egghead-link"49 href="https://egghead.io/lessons/javascript-produces-immutable-data-and-avoid-unnecessary-creation-of-new-data-trees-with-immer"50 >51 Hosted on egghead.io52 </a>53</details>54

fix: release and publish from 'main' rather than 'master' branchMichel Weststratebefore adoption82acc40

line 55why, one step back
55Here is a [simple benchmark](https://github.com/immerjs/immer/blob/main/__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.

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 56–71why, one step back
56
57Something 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.5859These tests were executed on Node 10.16.3. Use `yarn test:perf` to reproduce them locally.6061![performance.png](/img/performance.png)6263Most important observation:6465- 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.66- 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)67- Generating patches doesn't significantly slow down immer68- The ES5 fallback implementation is roughly twice as slow as the proxy implementation, in some cases worse.6970## Performance tips71

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 72–81why, one step back
72### Enable the Array Methods Plugin
7374For applications with significant array iteration within producers, enable the [Array Methods Plugin](./array-methods.md):7576```javascript77import {enableArrayMethods} from "immer"78enableArrayMethods()79```8081This plugin optimizes array operations like `filter`, `find`, `some`, `every`, and `slice` by avoiding proxy creation for every element during iteration. Without the plugin, iterating a 1000-element array creates 1000+ proxies. With the plugin, callbacks receive base values, and proxies are only created for elements you actually mutate.

[docs] Documented setUseStrictIterationMichel Weststratebefore adoptionbdf5cbd

lines 82–94why, one step back
82
83### Use loose iteration for better performance8485By default, Immer uses loose iteration which only processes enumerable string properties. This is faster than strict iteration which includes symbols and non-enumerable properties. For most use cases, the default is optimal:8687```javascript88import {setUseStrictIteration} from "immer"8990// Default: false (loose iteration for better performance)91setUseStrictIteration(false)92```9394Only enable strict iteration if you specifically need to track symbol or non-enumerable properties.

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

line 95why, one step back
95

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 96–110why, one step back
96### Pre-freeze data
9798When 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 `freeze(json)` on the root of the data that is being added first. To _shallowly_ freeze it. This will allow Immer to add the new data to the tree faster, as it will avoid the need to _recursively_ scan and freeze the new data.99100### You can always opt-out101102Realize 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.103104### For expensive search operations, read from the original state, not the draft105106Immer 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.107108### Pull produce as far up as possible109110Always 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)})`