Skip to content
Myr

readme.md

This file

Path

readme.md

425 lines · 17.3 KiB · Markdown

Last changed by

Merge pull request #156 from styfle/patch-1Michel Weststratebefore adoptione0a8a1a

History of this file

Take it

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 1–2why, one step back
1# Immer
2

Merge pull request #156 from styfle/patch-1Michel Weststratebefore adoptione0a8a1a

line 3why, one step back
3[![npm](https://img.shields.io/npm/v/immer.svg)](https://www.npmjs.com/package/immer) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js) [![install size](https://packagephobia.now.sh/badge?p=immer)](https://packagephobia.now.sh/result?p=immer) [![Build Status](https://travis-ci.org/mweststrate/immer.svg?branch=master)](https://travis-ci.org/mweststrate/immer) [![Coverage Status](https://coveralls.io/repos/github/mweststrate/immer/badge.svg?branch=master)](https://coveralls.io/github/mweststrate/immer?branch=master) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)

Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0

line 4why, one step back
4

Updated changelogMichel Weststratebefore adoption8f44348

line 5why, one step back
5_Create the next immutable state tree by simply modifying the current tree_

Doc improvementsMichel Weststratebefore adoptiona3fcf04

lines 6–8why, one step back
6
7---8

Merge pull request #123 from styfle/patch-1benbraoubefore adoption02fe6b1

lines 9–11why, one step back
9* NPM: `npm install immer`
10* Yarn: `yarn add immer`11* CDN: Exposed global is `immer`

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 12–13why, one step back
12 * Unpkg: `<script src="https://unpkg.com/immer/dist/immer.umd.js"></script>`
13 * JSDelivr: `<script src="https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js"></script>`

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 14why, one step back
14

Updated readme linksMichel Weststratebefore adoption553b6ce

lines 15–16why, one step back
15---
16

Object.is for equality, more tests. Fixes #43, #42, #48Michel Weststratebefore adoptione29a792

line 17why, one step back
17* Egghead lesson covering all of immer (7m): [Simplify creating immutable data trees with Immer](https://egghead.io/lessons/redux-simplify-creating-immutable-data-trees-with-immer)

Updated readme linksMichel Weststratebefore adoption553b6ce

lines 18–19why, one step back
18* Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)
19

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 20why, one step back
20Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way. It is based on the [_copy-on-write_](https://en.wikipedia.org/wiki/Copy-on-write) mechanism.

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 21why, one step back
21

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 22why, one step back
22The basic idea is that you will apply all your changes to a temporarily _draftState_, which is a proxy of the _currentState_. Once all your mutations are completed, Immer will produce the _nextState_ based on the mutations to the draft state. This means that you can interact with your data by simply modifying it, while keeping all the benefits of immutable data.

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 23why, one step back
23

Merge pull request #56 from jkhedani/masterMichel Weststratebefore adoption076fbd6

line 24why, one step back
24![immer-hd.png](images/hd/immer.png)

Doc improvementsMichel Weststratebefore adoptiona3fcf04

line 25why, one step back
25

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 26why, one step back
26Using Immer is like having a personal assistant; he takes a letter (the current state), and gives you a copy (draft) to jot changes onto. Once you are done, the assistant will take your draft and produce the real immutable, final letter for you (the next state).

Readme improvementsMichel Weststratebefore adoption58e9b57

line 27why, one step back
27

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 28why, one step back
28A mindful reader might notice that this is quite similar to `withMutations` of ImmutableJS. It is indeed, but generalized and applied to plain, native JavaScript data structures (arrays and objects) without further needing any library.

Doc improvementsMichel Weststratebefore adoptiona3fcf04

lines 29–31why, one step back
29
30## API31

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 32why, one step back
32The Immer package exposes a default function that does all the work.

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 33why, one step back
33

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 34why, one step back
34`produce(currentState, producer: (draftState) => void): nextState`

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 35why, one step back
35

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 36why, one step back
36There is also a curried overload that is explained [below](#currying).

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 37why, one step back
37

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 38–40why, one step back
38## Example
3940```javascript

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 41why, one step back
41import produce from "immer"

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 42why, one step back
42

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 43–48why, one step back
43const baseState = [
44 {45 todo: "Learn typescript",46 done: true47 },48 {

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 49why, one step back
49 todo: "Try immer",

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 50–53why, one step back
50 done: false
51 }52]53

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 54why, one step back
54const nextState = produce(baseState, draftState => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 55why, one step back
55 draftState.push({todo: "Tweet about it"})

Doc improvementsMichel Weststratebefore adoptiona3fcf04

line 56why, one step back
56 draftState[1].done = true

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 57–59why, one step back
57})
58```59

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 60why, one step back
60The interesting thing about Immer is that the `baseState` will be untouched, but the `nextState` will reflect all changes made to `draftState`.

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 61–76why, one step back
61
62```javascript63// the new item is only added to the next state,64// base state is unmodified65expect(baseState.length).toBe(2)66expect(nextState.length).toBe(3)6768// same for the changed 'done' prop69expect(baseState[1].done).toBe(false)70expect(nextState[1].done).toBe(true)7172// unchanged data is structurally shared73expect(nextState[0]).toBe(baseState[0])74// changed data not (dûh)75expect(nextState[1]).not.toBe(baseState[1])76```

Doc improvementsMichel Weststratebefore adoptiona3fcf04

lines 77–79why, one step back
77
78## Benefits79

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 80–85why, one step back
80* Immutability with normal JavaScript objects and arrays. No new APIs to learn!
81* Strongly typed, no string based paths selectors etc.82* Structural sharing out of the box83* Object freezing out of the box84* Deep updates are a breeze85* Boilerplate reduction. Less noise, more concise code.

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 86why, one step back
86* Small: bundled and minified: 2KB.

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 87why, one step back
87

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 88why, one step back
88Read further to see all these benefits explained.

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 89why, one step back
89

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 90–91why, one step back
90## Reducer Example
91

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 92why, one step back
92Here is a simple example of the difference that Immer could make in practice.

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 93–94why, one step back
93
94```javascript

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 95–97why, one step back
95// Redux reducer
96// Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.js97const byId = (state, action) => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 98–109why, one step back
98 switch (action.type) {
99 case RECEIVE_PRODUCTS:100 return {101 ...state,102 ...action.products.reduce((obj, product) => {103 obj[product.id] = product104 return obj105 }, {})106 }107 default:108 return state109 }

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 110–112why, one step back
110}
111```112

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 113why, one step back
113After using Immer, that simply becomes:

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 114–115why, one step back
114
115```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 116why, one step back
116import produce from "immer"

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 117why, one step back
117

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 118why, one step back
118const byId = (state, action) =>

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 119–126why, one step back
119 produce(state, draft => {
120 switch (action.type) {121 case RECEIVE_PRODUCTS:122 action.products.forEach(product => {123 draft[product.id] = product124 })125 }126 })

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 127–128why, one step back
127```
128

transpile es5 codeMichel Weststratebefore adoptionf012154

line 129why, one step back
129Notice that it is not needed to handle the default case, a producer that doesn't do anything will simply return the original state.

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 130why, one step back
130

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 131why, one step back
131Creating Redux reducer is just a sample application of the Immer package. Immer is not just designed to simplify Redux reducers. It can be used in any context where you have an immutable data tree that you want to clone and modify (with structural sharing).

transpile es5 codeMichel Weststratebefore adoptionf012154

line 132why, one step back
132

Merge pull request #114 from xaviergonz/patch-1Michel Weststratebefore adoptionc71b173

line 133why, one step back
133_Note: it might be tempting after using producers for a while, to just place `produce` in your root reducer and then pass the draft to each reducer and work directly over such draft. Don't do that. It kills the point of Redux where each reducer is testable as pure reducer. Immer is best used when applying it to small individual pieces of logic._

Added examples from the Redux Update Patterns PageMichel Weststratebefore adoptionda3a08b

line 134why, one step back
134

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 135–136why, one step back
135## React.setState example
136

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 137why, one step back
137Deep updates in the state of React components can be greatly simplified as well by using immer. Take for example the following onClick handlers (Try in [codesandbox](https://codesandbox.io/s/m4yp57632j)):

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 138–143why, one step back
138
139```javascript140/**141 * Classic React.setState with a deep merge142 */143onBirthDayClick1 = () => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 144why, one step back
144 this.setState(prevState => ({

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

line 145why, one step back
145 user: {

Merge pull request #104 from mikollo/patch-1Michel Weststratebefore adoptionc047a59

lines 146–147why, one step back
146 ...prevState.user,
147 age: prevState.user.age + 1

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

line 148why, one step back
148 }

Merge pull request #104 from mikollo/patch-1Michel Weststratebefore adoptionc047a59

line 149why, one step back
149 }))

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 150–153why, one step back
150}
151152/**153 * ...But, since setState accepts functions,

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 154why, one step back
154 * we can just create a curried producer and further simplify!

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 155why, one step back
155 */

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

line 156why, one step back
156onBirthDayClick2 = () => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 157–161why, one step back
157 this.setState(
158 produce(draft => {159 draft.user.age += 1160 })161 )

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 162–164why, one step back
162}
163```164

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 165–166why, one step back
165## Currying
166

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 167why, one step back
167Passing a function as the first argument to `produce` is intended to be used for currying. This means that you get a pre-bound producer that only needs a state to produce the value from. The producer function gets passed in the draft, and any further arguments that were passed to the curried function.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 168–184why, one step back
168
169For example:170171```javascript172// mapper will be of signature (state, index) => state173const mapper = produce((draft, index) => {174 draft.index = index175})176177// example usage178console.dir([{}, {}, {}].map(mapper))179//[{index: 0}, {index: 1}, {index: 2}])180```181182This mechanism can also nicely be leveraged to further simplify our example reducer:183184```javascript

Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26

lines 185–186why, one step back
185import produce from 'immer'
186

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 187–189why, one step back
187const byId = produce((draft, action) => {
188 switch (action.type) {189 case RECEIVE_PRODUCTS:

Merge pull request #71 from Keysox/patch-1Michel Weststratebefore adoptiond47db4d

line 190why, one step back
190 action.products.forEach(product => {

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 191why, one step back
191 draft[product.id] = product

Merge pull request #71 from Keysox/patch-1Michel Weststratebefore adoptiond47db4d

line 192why, one step back
192 })

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 193why, one step back
193 return

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 194–200why, one step back
194 })
195 }196})197```198199Note that `state` is now factored out (the created reducer will accept a state, and invoke the bound producer with it).200

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 201–203why, one step back
201If you want to initialize an uninitialized state using this construction, you can do so by passing the initial state as second argument to `produce`:
202203```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 204why, one step back
204import produce from "immer"

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 205why, one step back
205

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 206why, one step back
206const byId = produce(

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 207–217why, one step back
207 (draft, action) => {
208 switch (action.type) {209 case RECEIVE_PRODUCTS:210 action.products.forEach(product => {211 draft[product.id] = product212 })213 return214 }215 },216 {217 1: {id: 1, name: "product-1"}

Readme improvementsMichel Weststratebefore adoptiondcedda8

lines 218–219why, one step back
218 }
219)

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 220–221why, one step back
220```
221

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 222–223why, one step back
222## Auto freezing
223

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 224why, one step back
224Immer automatically freezes any state trees that are modified using `produce`. This protects against accidental modifications of the state tree outside of a producer. This comes with a performance impact, so it is recommended to disable this option in production. It is by default enabled. By default it is turned on during local development, and turned off in production. Use `setAutoFreeze(true / false)` to explicitly turn this feature on or off.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 225why, one step back
225

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 226–227why, one step back
226## Returning data from producers
227

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 228why, one step back
228It is not needed to return anything from a producer, as Immer will return the (finalized) version of the `draft` anyway. However, it is allowed to just `return draft`.

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 229why, one step back
229

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 230why, one step back
230It is also allowed to return arbitrarily other data from the producer function. But _only_ if you didn't modify the draft. This can be useful to produce an entirely new state. Some examples:

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 231–233why, one step back
231
232```javascript233const userReducer = produce((draft, action) => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 234–263why, one step back
234 switch (action.type) {
235 case "renameUser":236 // OK: we modify the current state237 draft.users[action.payload.id].name = action.payload.name238 return draft // same as just 'return'239 case "loadUsers":240 // OK: we return an entirely new state241 return action.payload242 case "adduser-1":243 // NOT OK: This doesn't do change the draft nor return a new state!244 // It doesn't modify the draft (it just redeclares it)245 // In fact, this just doesn't do anything at all246 draft = {users: [...draft.users, action.payload]}247 return248 case "adduser-2":249 // NOT OK: modifying draft *and* returning a new state250 draft.userCount += 1251 return {users: [...draft.users, action.payload]}252 case "adduser-3":253 // OK: returning a new state. But, unnecessary complex and expensive254 return {255 userCount: draft.userCount + 1,256 users: [...draft.users, action.payload]257 }258 case "adduser-4":259 // OK: the immer way260 draft.userCount += 1261 draft.users.push(action.payload)262 return263 }

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 264–266why, one step back
264})
265```266

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 267–273why, one step back
267## Using `this`
268269The recipe will be always invoked with the `draft` as `this` context.270271This means that the following constructions are also valid:272273```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 274why, one step back
274const base = {counter: 0}

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 275–276why, one step back
275
276const next = produce(base, function() {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 277why, one step back
277 this.counter++

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 278–282why, one step back
278})
279console.log(next.counter) // 1280281// OR282const increment = produce(function() {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 283why, one step back
283 this.counter++

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 284–291why, one step back
284})
285console.log(increment(base).counter) // 1286```287288## TypeScript or Flow289290The 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.291

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 292why, one step back
292## Immer on older JavaScript environments?

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 293why, one step back
293

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 294why, one step back
294By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native on Android. In such cases Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 295–296why, one step back
295
296## Pitfalls

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 297why, one step back
297

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 298why, one step back
2981. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](#returning-data-from-producers).

Removed superfluos Redux examples (fixes #57). Explained working with Dates betterMichel Weststratebefore adoptiona7b5df7

line 299why, one step back
2991. Currently, Immer only supports plain objects and arrays. PRs are welcome for more language built-in types like `Map` and `Set`.

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 300–303why, one step back
3001. Immer only processes native arrays and plain objects (with a prototype of `null` or `Object`). Any other type of value will be treated verbatim! So if you modify a `Map` or `Buffer` (or whatever complex object from the draft state), the changes will be persisted. But, both in your new and old state! So, in such cases, make sure to always produce fresh instances if you want to keep your state truly immutable.
3011. For example, working with `Date` objects is no problem, just make sure you never modify them (by using methods like `setYear` on an existing instance). Instead, always create fresh `Date` instances. Which is probably what you were unconsciously doing already.3021. Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`3031. Some debuggers (at least Node 6 is known) have trouble debugging when Proxies are in play. Node 8 is known to work correctly.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 304why, one step back
304

Added section with cool stuffMichel Weststratebefore adoption69aedd8

lines 305–306why, one step back
305## Cool things built with immer
306

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 307why, one step back
307* [redux-starter-kit](https://github.com/markerikson/redux-starter-kit) _A simple set of tools to make using Redux easier_

Added section with cool stuffMichel Weststratebefore adoption69aedd8

lines 308–309why, one step back
308* [immer based handleActions](https://gist.github.com/kitze/fb65f527803a93fb2803ce79a792fff8) _Boilerplate free actions for Redux_
309* [redux-box](https://github.com/anish000kumar/redux-box) _Modular and easy-to-grasp redux based state management, with least boilerplate_

Merge pull request #122 from jeffreyyoung/patch-1Michel Weststratebefore adoption1b381ff

line 310why, one step back
310* [quick-redux](https://github.com/jeffreyyoung/quick-redux) _tools to make redux developement quicker and easier_

updated readmeMichel Weststratebefore adoptionfb81324

line 311why, one step back
311* [react-copy-write](https://github.com/aweary/react-copy-write) _Immutable state with a mutable API_

Added section with cool stuffMichel Weststratebefore adoption69aedd8

line 312why, one step back
312

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 313–314why, one step back
313## How does Immer work?
314

Object.is for equality, more tests. Fixes #43, #42, #48Michel Weststratebefore adoptione29a792

line 315why, one step back
315Read the (second part of the) [introduction blog](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3).

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 316why, one step back
316

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 317–320why, one step back
317## Example patterns.
318319_For those who have to go back to thinking in object updates :-)_320

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 321why, one step back
321```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 322why, one step back
322import produce from "immer"

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 323–325why, one step back
323
324// object mutations325const todosObj = {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 326–328why, one step back
326 id1: {done: false, body: "Take out the trash"},
327 id2: {done: false, body: "Check Email"}328}

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 329–331why, one step back
329
330// add331const addedTodosObj = produce(todosObj, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 332–333why, one step back
332 draft["id3"] = {done: false, body: "Buy bananas"}
333})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 334–336why, one step back
334
335// delete336const deletedTodosObj = produce(todosObj, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 337–338why, one step back
337 delete draft["id1"]
338})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 339–341why, one step back
339
340// update341const updatedTodosObj = produce(todosObj, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 342–343why, one step back
342 draft["id1"].done = true
343})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 344–346why, one step back
344
345// array mutations346const todosArray = [

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 347–349why, one step back
347 {id: "id1", done: false, body: "Take out the trash"},
348 {id: "id2", done: false, body: "Check Email"}349]

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 350–352why, one step back
350
351// add352const addedTodosArray = produce(todosArray, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 353–354why, one step back
353 draft.push({id: "id3", done: false, body: "Buy bananas"})
354})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 355–357why, one step back
355
356// delete357const deletedTodosArray = produce(todosArray, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 358–361why, one step back
358 draft.splice(draft.findIndex(todo => todo.id === "id1"), 1)
359 // or (slower):360 // return draft.filter(todo => todo.id !== "id1")361})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 362–364why, one step back
362
363// update364const updatedTodosArray = produce(todosArray, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 365–366why, one step back
365 draft[draft.findIndex(todo => todo.id === "id1")].done = true
366})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 367–368why, one step back
367```
368

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 369–370why, one step back
369## Performance
370

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 371why, one step back
371Here is a [simple benchmark](__performance_tests__/todo.js) on the performance of Immer. This test takes 100.000 todo items, and updates 10.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.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 372why, one step back
372

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 373why, one step back
373These tests were executed on Node 8.4.0. Use `yarn test:perf` to reproduce them locally.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 374why, one step back
374

Fixed performance imagesMichel Weststratebefore adoption89e0d24

line 375why, one step back
375![performance.png](images/performance.png)

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 376–377why, one step back
376
377Some observations:

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 378why, one step back
378

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

line 379why, one step back
379* 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.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 380why, one step back
380* 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).

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 381why, one step back
381* 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.

Fixed #49Michel Weststratebefore adoption5227173

line 382why, one step back
382* Immer with proxies is roughly speaking twice as slow as a hand written reducer. This is in practice negligible.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 383why, one step back
383* 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)

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 384why, one step back
384* The ES5 implementation of Immer is significantly slower. For most reducers this won't matter, but reducers that process large amounts of data might benefit from not (or only partially) using an Immer producer. Luckily, Immer is fully opt-in.

Merge pull request #154 from knpwrs/patch-1benbraoubefore adoption26f22e9

line 385why, one step back
385* The peaks in the _frozen_ versions of _just mutate_, _deepclone_ and _naive reducer_ come from the fact that they recursively freeze the full state tree, while the other test cases only freeze the modified parts of the tree.

Added performance measurements, fixes #6Michel Weststratebefore adoption553299f

line 386why, one step back
386

Added FAQ sectionMichel Weststratebefore adoptionb9b418e

lines 387–414why, one step back
387## FAQ
388389_(for those who skimmed the above instead of actually reading)_390391**Q: Does Immer use structural sharing? So that my selectors can be memoized and such?**392393A: Yes394395**Q: Does Immer support deep updates?**396397A: Yes398399**Q: I can't rely on Proxies being present on my target environments. Can I use Immer?**400401A: Yes402403**Q: Can I typecheck my data structures when using Immer?**404405A: Yes406407**Q: Can I store `Date` objects, functions etc in my state tree when using Immer?**408409A: Yes410411**Q: Is it fast?**412413A: Yes414

Added question about freezingMichel Weststratebefore adoption6ac7707

lines 415–418why, one step back
415**Q: Idea! Can Immer freeze the state for me?**
416417A: Yes418

Added performance measurements, fixes #6Michel Weststratebefore adoption553299f

lines 419–421why, one step back
419## Credits
420421Special thanks goes to @Mendix, which supports it's employees to experiment completely freely two full days a month, which formed the kick-start for this project.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 422–425why, one step back
422
423## Donations424425A significant part of my OSS work is unpaid. So [donations](https://mobx.js.org/donate.html) are greatly appreciated :)