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
Provenance
- Commit
553299f- Tree
4d999c5- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
3c746c0
5 files changed+124 added−1 removed
added:__tests__/performance.js+92 added
1+"use strict"2+import immer, {setAutoFreeze} from ".."3+import cloneDeep from "lodash.clonedeep"4+import {List, Record} from "immutable"5+6+describe("performance", () => {7+ const MAX = 1000008+ const MODIFY_FACTOR = 0.19+ const baseState = []10+ let frozenBazeState11+ let immutableJsBaseState12+13+ // 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+ }21+22+ // 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)31+32+ // generate immutalbeJS base state33+ const todoRecord = Record({34+ todo: "",35+ done: false,36+ someThingCompletelyIrrelevant: [],37+ })38+ immutableJsBaseState = List(baseState.map(todo => todoRecord(todo)))39+40+ // The benchmarks41+42+ test("just mutate", () => {43+ for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {44+ baseState[i].done = true45+ }46+ })47+48+ 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+ })54+55+ 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+ })64+65+ 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+ })73+74+ 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+ })82+83+ 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 lines168 !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 linesmodified:package.json+2 added
32 unmodified lines33343536373839404142437 unmodified lines32 unmodified lines "@types/jest": "^22.0.0", "babel-core": "^6.26.0", "babel-jest": "^22.0.4", "babel-preset-env": "^1.6.1", "immutable": "^3.8.2", "jest": "^22.0.4", "lodash.clonedeep": "^4.5.0", "pre-commit": "^1.2.2", "prettier": "^1.9.2", "regenerator-runtime": "^0.11.1", "typescript": "^2.6.2"7 unmodified linesmodified:readme.md+21 added-1 removed
148 unmodified lines149 ```150 151 Creating middleware or a reducer wrapper that applies `immer` automatically is left as exercise for the reader :-).152 153+## Performance154+155+Here is a [simple benchmark](__tests__/performance.js) on the performance of `immer`.156+This test takes 100.000 todo items, and updates 10.000 of them.157+These tests were executed on Node 8.4.0158+159+```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+```168+169 ## Limitations170 155-* 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)174 175 ## Pitfalls:176 177 * 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`179+180+## Credits181+182+Special 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 lines1401 iconv-lite@0.4.19:1402 version "0.4.19"1403 resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"1404 1405+immutable@^3.8.2:1406+ version "3.8.2"1407+ resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"1408+1409 imurmurhash@^0.1.4:1410 version "0.1.4"1411 resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"1412 599 unmodified lines2012 dependencies:2013 p-locate "^2.0.0"2014 path-exists "^3.0.0"2015 2016+lodash.clonedeep@^4.5.0:2017+ version "4.5.0"2018+ resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"2019+2020 lodash.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