[docs] Documented setUseStrictIteration
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 d38a9d0 · Browse files · 2 files·+16 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+16 added−4 removed
- other:+16 added−4 removed
Provenance
- Commit
bdf5cbd- Tree
4923bec- Author
Michel Weststrate <mweststrate@meta.com>- Committer
Michel Weststrate <mweststrate@meta.com>- Parent
d38a9d0
2 files changed+16 added−4 removed
modified:website/docs/api.md+1 added
30 unmodified lines3132333435363738396 unmodified lines30 unmodified lines| `Patch` | Exposed TypeScript type, describes the shape of an (inverse) patch object | [Patches](./patches.mdx) || `produceWithPatches` | Works the same as `produce`, but instead of just returning the produced object, it returns a tuple, consisting of `[result, patches, inversePatches]`. | [Patches](./patches.mdx) || `setAutoFreeze` | Enables / disables automatic freezing of the trees produces. By default enabled. | [Freezing](./freezing.mdx) || `setUseStrictShallowCopy` | Can be used to enable strict shallow copy. If enable, immer copies non-enumerable properties as much as possible. | [Classes](./complex-objects.md) || `setUseStrictIteration` | Controls iteration behavior: pass `false` for loose iteration (enumerable properties only, better performance) or `true` for strict iteration (includes symbols and non-enumerable properties). By default disabled (loose iteration). | |
## Importing immer
In most cases, the only thing you need to import from Immer is `produce`:6 unmodified linesmodified:website/docs/performance.mdx+15 added-4 removed
7 unmodified lines8 data-ea-publisher="immerjs"9 data-ea-type="image"10 className="horizontal bordered"11 ></div>12-</center>13-<details>12+</center> <details>13 <summary className="egghead-summary">14 egghead.io lesson 5: Leveraging Immer's structural sharing in React15 </summary>16 <br />10 unmodified lines27 href="https://egghead.io/lessons/react-profile-react-rendering-and-optimize-with-memo-to-leverage-structural-sharing"28 >29 Hosted on egghead.io30 </a>32-</details>33-<details>31+</details> <details>32 <summary className="egghead-summary">33 egghead.io lesson 7: Immer will try to re-cycle data if there was no34 semantic change35 </summary>41 unmodified lines77 ```78 79 This 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.80 81+### Use loose iteration for better performance82+83+By 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:84+85+```javascript86+import {setUseStrictIteration} from "immer"87+88+// Default: false (loose iteration for better performance)89+setUseStrictIteration(false)90+```91+92+Only enable strict iteration if you specifically need to track symbol or non-enumerable properties.93+94 ### Pre-freeze data95 96 When 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.97 11 unmodified lines