Skip to content
Myr

Added performance measurements, fixes #6

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 3c746c0 · Browse files · 5 files+124 added−1 removed
This commit

State

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

Diff stats

5 files+124 added−1 removed

  • tests:+92 added−0 removed
  • other:+32 added−1 removed

5 files+124 added−1 removed

added:__tests__/performance.js+92 added
1"use strict"2import immer, {setAutoFreeze} from ".."3import cloneDeep from "lodash.clonedeep"4import {List, Record} from "immutable"56describe("performance", () => {7 const MAX = 1000008 const MODIFY_FACTOR = 0.19 const baseState = []10 let frozenBazeState11 let immutableJsBaseState1213 // produce the base state14 for (let i = 0; i < MAX; i++) {15 baseState.push({16 todo: "todo_" + i,17 done: false,18 someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],19 })20 }2122 // Produce the frozen bazeState23 frozenBazeState = baseState.map(todo => {24 const newTodo = {...todo}25 newTodo.someThingCompletelyIrrelevant = todo.someThingCompletelyIrrelevant.slice()26 Object.freeze(newTodo.someThingCompletelyIrrelevant)27 Object.freeze(newTodo)28 return newTodo29 })30 Object.freeze(frozenBazeState)3132 // generate immutalbeJS base state33 const todoRecord = Record({34 todo: "",35 done: false,36 someThingCompletelyIrrelevant: [],37 })38 immutableJsBaseState = List(baseState.map(todo => todoRecord(todo)))3940 // The benchmarks4142 test("just mutate", () => {43 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {44 baseState[i].done = true45 }46 })4748 test("deepclone, then mutate", () => {49 const draft = cloneDeep(baseState)50 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {51 draft[i].done = true52 }53 })5455 test("handcrafted reducer", () => {56 const nextState = [57 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo => ({58 ...todo,59 done: true,60 })),61 ...baseState.slice(MAX * MODIFY_FACTOR),62 ]63 })6465 test("immutableJS", () => {66 let state = immutableJsBaseState67 state.withMutations(state => {68 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {69 state.setIn([i, "done"], true)70 }71 })72 })7374 test("immer - with autofreeze", () => {75 setAutoFreeze(true)76 immer(frozenBazeState, draft => {77 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {78 draft[i].done = true79 }80 })81 })8283 test("immer - without autofreeze", () => {84 setAutoFreeze(false)85 immer(baseState, draft => {86 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {87 draft[i].done = true88 }89 })90 setAutoFreeze(true)91 })92})
modified:immer.js+1 added
167 unmodified lines
168 !Object.is(maybeVoidReturn, undefined) &&169 console.warn(170 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`,171 )172 // console.log(`proxies: ${revocableProxies.size}, copies: ${copies.size}`)173 // revoke all proxies174 revoke(revocableProxies)175 // and finalize the modified proxy176 return finalize(baseState)
47 unmodified lines
modified:package.json+2 added
modified:readme.md+21 added-1 removed
148 unmodified lines
149```150151Creating middleware or a reducer wrapper that applies `immer` automatically is left as exercise for the reader :-).152153## Performance154155Here is a [simple benchmark](__tests__/performance.js) on the performance of `immer`.156This test takes 100.000 todo items, and updates 10.000 of them.157These tests were executed on Node 8.4.0158159```160 performance161 ✓ just mutate (1ms) // No immutability at all162 ✓ deepclone, then mutate (647ms) // Clone entire tree, then mutate (no structural sharing!)163 ✓ handcrafted reducer (17ms) // Implement it as typical Redux reducer, with slices and spread operator164 ✓ immutableJS (81ms) // Use immutableJS and leverage `withMutations` for best performance165 ✓ immer - with autofreeze (309ms) // Immer, with auto freeze enabled166 ✓ immer - without autofreeze (148ms) // Immer, but without auto freeze enabled167```168169## Limitations170155* This package requires Proxies, so Safari > 10, no Internet Explorer171* This package requires Proxies, so Safari > 9, no Internet Explorer, no React Native on Android. This can potentially done, so feel free to upvote on [#8](https://github.com/mweststrate/immer/issues/8) if you need this :)172* Currently, only tree shaped states are supported. Cycles could potentially be supported as well (PR's welcome)173* Currently, only supports plain objects and arrays. Non-plain data structures (like `Map`, `Set`) not (yet). (PR's welcome)174175## Pitfalls:176177* Make sure to modify the draft state you get passed in in the callback function, not the original current state that was passed as the first argument to `immer`!178* Since immer uses proxies, reading huge amounts of data from state comes with an overhead. If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the `immer` block or read from the `currentState` rather than the `draftState`179180## Credits181182Special 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.
modified:yarn.lock+8 added
1400 unmodified lines
1401iconv-lite@0.4.19:1402 version "0.4.19"1403 resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"14041405immutable@^3.8.2:1406 version "3.8.2"1407 resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"14081409imurmurhash@^0.1.4:1410 version "0.1.4"1411 resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"1412
599 unmodified lines
2012 dependencies:2013 p-locate "^2.0.0"2014 path-exists "^3.0.0"20152016lodash.clonedeep@^4.5.0:2017 version "4.5.0"2018 resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"20192020lodash.sortby@^4.7.0:2021 version "4.7.0"2022 resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"2023
1094 unmodified lines