Skip to content

Commit bdf5cbd

Browse files
author
Michel Weststrate
committed
[docs] Documented setUseStrictIteration
1 parent d38a9d0 commit bdf5cbd

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

website/docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ title: API overview
3232
| `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) |
3333
| `setAutoFreeze` | Enables / disables automatic freezing of the trees produces. By default enabled. | [Freezing](./freezing.mdx) |
3434
| `setUseStrictShallowCopy` | Can be used to enable strict shallow copy. If enable, immer copies non-enumerable properties as much as possible. | [Classes](./complex-objects.md) |
35+
| `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). | |
3536

3637
## Importing immer
3738

website/docs/performance.mdx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ title: Immer performance
99
data-ea-type="image"
1010
className="horizontal bordered"
1111
></div>
12-
</center>
13-
<details>
12+
</center> <details>
1413
<summary className="egghead-summary">
1514
egghead.io lesson 5: Leveraging Immer's structural sharing in React
1615
</summary>
@@ -29,8 +28,7 @@ title: Immer performance
2928
>
3029
Hosted on egghead.io
3130
</a>
32-
</details>
33-
<details>
31+
</details> <details>
3432
<summary className="egghead-summary">
3533
egghead.io lesson 7: Immer will try to re-cycle data if there was no
3634
semantic change
@@ -80,6 +78,19 @@ enableArrayMethods()
8078

8179
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.
8280

81+
### Use loose iteration for better performance
82+
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+
```javascript
86+
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+
8394
### Pre-freeze data
8495

8596
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.

0 commit comments

Comments
 (0)