Skip to content
Myr

Documented 1.0.0 release!

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 7d2148c · Browse files · 4 files+50 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+50 added−3 removed

  • tests:+15 added−0 removed
  • other:+35 added−3 removed

4 files+50 added−3 removed

modified:__tests__/readme.js+15 added
modified:changelog.md+7 added
modified:package.json+1 added-1 removed
1{2 "name": "immer",3 "version": "0.8.5",3 "version": "1.0.0",4 "description": "Create your next immutable state by mutating the current one",5 "main": "dist/immer.js",6 "umd:main": "dist/immer.umd.js",7 "module": "dist/immer.module.js",
72 unmodified lines
modified:readme.md+27 added-2 removed
173 unmodified lines
174175Note that `state` is now factored out (the created reducer will accept a state, and invoke the bound producer with it).176One think to keep in mind; you cannot use this construction to initialize an uninitialized state. E.g. `draft = {}` doesn't do anything useful.177178178## Auto freezing179180 Immer automatically freezes any state trees that are modified using `produce`.181 This protects against accidental modifications of the state tree outside of a producer.182 This comes with a performance impact, so it is recommended to disable this option in production.183 It is by default enabled.185 Use `setAutoFreeze(true / false)` to turn this feature on or off.184 By default it is turned on during local development, and turned off in production.185 Use `setAutoFreeze(true / false)` to explicitly turn this feature on or off.186187## Using `this`188189The recipe will be always invoked with the `draft` as `this` context.190191This means that the following constructions are also valid:192193```javascript194const base = { counter: 0 }195196const next = produce(base, function() {197 this.counter++198})199console.log(next.counter) // 1200201// OR202const increment = produce(function() {203 this.counter++204})205console.log(increment(base).counter) // 1206```207208## TypeScript or Flow209210The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.211212## Immer on older JavaScript environments?213214By default `produce` tries to use proxies for optimal performance.215However, on older JavaScript engines `Proxy` is not available.
23 unmodified lines
239240![performance.png](images/performance.png)241242Some observations:243* From `immer` perspective, this benchmark is a _worst case_ scenario, because the root collection it has to proxy is really large relatively to the rest of the data set.244* The _mutate_, and _deepclone, mutate_ benchmarks establish a baseline on how expensive changing the data is, without immutability (or structural sharing in the deep clone case).245* The _reducer_ and _naive reducer_ are implemented in typical Redux style reducers. The "smart" implementation slices the collection first, and then maps and freezes only the relevant todos. The "naive" implementation just maps over and processes the entire collection.246* Immer with proxies is roughly speaking twice as slow as a hand written reducer. This is in practice negligible.247* 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)
42 unmodified lines