npm immer 11.2.14
v11.2.14

one hour ago

This bugfix release makes Immer significantly faster when updating large frozen arrays and storing new data, fixes useStrictIteration being ignored during finalization, and changes current() to share untouched parts of the original state.

Changelog

Performance Improvements

We've expanded the project's benchmark suite to cover more realistic scenarios: frozen initial state (which is what you have in a real Redux store), chained updates, 10k-item arrays, and storing large API responses as RTK Query does. Running it showed several places where Immer was doing far more work than it needed to.

Freezing: Immer used to deep-freeze the entire result after every produce call. On a large state, that meant walking every object in the tree, even ones that had not changed and were already frozen. Immer now freezes each modified draft's copy as it finalizes, and walks only the values that are actually new. Toggling one item in a frozen 10k-item array dropped from about 1ms to 11µs.

Copying frozen arrays: Copying a frozen array with slice() turned out to be much slower in V8 than copying with spread syntax. shallowCopy now uses spread for frozen arrays.

Proxy traps: The get trap now returns primitives before running the isDraftable checks. The set trap skips the prototype walk for own properties. Array drafts call the object traps directly instead of going through an arguments rewrite. Updates that go through the proxy array, like find() followed by an assignment, are 10-45% faster.

enableArrayMethods reordering: After splice, shift, unshift, sort, or reverse, the array methods plugin built a Set of every base item. That made each of those calls O(n), even when nothing read the array afterward. The plugin now builds that Set only when it's needed. Removing one item from a 10k-item array is now about 30-40x faster.

Walking new data: When you assign new objects into a draft, finalize walks them to find nested drafts and freeze the data. That walk now skips primitives early and uses index loops instead of forEach closures. It also uses Object.keys instead of Reflect.ownKeys, which is what the default loose iteration mode always intended (see the fix below). Storing a newly fetched 1k-item API response is about 2x faster.

Combined, comparing 11.2.13 to 11.2.14 with auto-freeze on:

Scenario 11.2.13 11.2.14 Speedup
10k array: toggle one item 1.04ms 11.4µs 91x
10k array: 10 chained toggles 9.92ms 111µs 89x
10k array: add an item 33x
10k array: update via find 2.7x
100 items: update 8.9x
RTKQ-style action sequence 4.8x
Store new 1k-item API response 2.0x
10k array: remove item (enableArrayMethods) 143x
10k array: 10 chained removes (enableArrayMethods) 209x

With auto-freeze off, most scenarios are 1.1-1.5x faster. We didn't see any scenario get clearly slower. Our measurements vary by up to ~20% between runs, so treat the smaller numbers as approximate.

useStrictIteration Fix

Immer 10.2 added setUseStrictIteration(). In strict mode, Immer looks for drafts nested under symbol and non-enumerable keys when it walks newly assigned objects. Immer 11.0 made loose iteration the default. Turns out the same release also rewrote the finalize walk, and the rewrite dropped the setting. (Oops!)
Since 11.0, that walk always used strict iteration, whatever you configured. The setting only affected current().

The walk now follows the setting again. With the default loose mode, you get the faster Object.keys iteration that was intended all along. If you put drafts inside new objects under symbol or non-enumerable keys, call setUseStrictIteration(true) to keep them finalized.

current() Structural Sharing

current() used to deep-copy every part of the draft tree that wasn't frozen, including parts you never touched. With auto-freeze off and a large state, one current() call could take tens of milliseconds. current() now returns untouched parts of the original state as is. The current docs already described this behavior.

If you mutate the result of current(), you may now be mutating objects that are also part of your original state, when that state isn't frozen. Treat the result as read-only, or copy the parts you plan to change.

Other Changes

The draft set trap now writes to an own data property even if a prototype in the chain defines a setter with the same name. Previously Immer called the prototype setter, which doesn't match normal JS semantics.

What's Changed

  • Benchmark pre-frozen chained state and 10k-item arrays in perf-testing by @markerikson in #1388
  • chore: Benchmark updates: drop Immer 10, add large API response scenarios by @markerikson in #1390
  • fix: Honor useStrictIteration when finalize walks newly assigned values by @markerikson in #1391
  • perf: Freeze each draft copy during finalize instead of deep-freezing the result by @markerikson in #1392
  • perf: Copy frozen arrays with spread instead of slice by @markerikson in #1393
  • perf: Faster proxy traps and array reordering, and structural sharing in current() by @markerikson in #1394
  • perf: Faster finalize walk over newly assigned data by @markerikson in #1395

Full Changelog: v11.2.13...v11.2.14

Don't miss a new immer release

NewReleases is sending notifications on new releases.