performance.js
This file
Path
__tests__/performance.js
112 lines · 3.1 KiB · JavaScript
Last changed by
Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
History of this fileTake it
Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
line 1why, one step back1"use strict"Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 2–3why, one step back2import immerProxy, {setAutoFreeze as setAutoFreezeProxy} from ".."3import immerEs5, {setAutoFreeze as setAutoFreezeEs5} from "../es5"Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
lines 4–18why, one step back4import cloneDeep from "lodash.clonedeep"5import {List, Record} from "immutable"67describe("performance", () => {8 const MAX = 1000009 const MODIFY_FACTOR = 0.110 const baseState = []11 let frozenBazeState12 let immutableJsBaseState1314 // produce the base state15 for (let i = 0; i < MAX; i++) {16 baseState.push({17 todo: "todo_" + i,18 done: false,No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 19why, one step back19 someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
lines 20–36why, one step back20 })21 }2223 // Produce the frozen bazeState24 frozenBazeState = baseState.map(todo => {25 const newTodo = {...todo}26 newTodo.someThingCompletelyIrrelevant = todo.someThingCompletelyIrrelevant.slice()27 Object.freeze(newTodo.someThingCompletelyIrrelevant)28 Object.freeze(newTodo)29 return newTodo30 })31 Object.freeze(frozenBazeState)3233 // generate immutalbeJS base state34 const todoRecord = Record({35 todo: "",36 done: false,No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 37why, one step back37 someThingCompletelyIrrelevant: []Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
lines 38–59why, one step back38 })39 immutableJsBaseState = List(baseState.map(todo => todoRecord(todo)))4041 // The benchmarks4243 test("just mutate", () => {44 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {45 baseState[i].done = true46 }47 })4849 test("deepclone, then mutate", () => {50 const draft = cloneDeep(baseState)51 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {52 draft[i].done = true53 }54 })5556 test("handcrafted reducer", () => {57 const nextState = [58 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo => ({59 ...todo,No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 60why, one step back60 done: trueAdded performance measurements, fixes #6Michel Weststratebefore adoption553299f
line 61why, one step back61 })),No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 62why, one step back62 ...baseState.slice(MAX * MODIFY_FACTOR)Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
lines 63–74why, one step back63 ]64 })6566 test("immutableJS", () => {67 let state = immutableJsBaseState68 state.withMutations(state => {69 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {70 state.setIn([i, "done"], true)71 }72 })73 })74Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 75–77why, one step back75 test("immer (proxy) - with autofreeze", () => {76 setAutoFreezeProxy(true)77 immerProxy(frozenBazeState, draft => {Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
lines 78–83why, one step back78 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {79 draft[i].done = true80 }81 })82 })83Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 84–86why, one step back84 test("immer (proxy) - without autofreeze", () => {85 setAutoFreezeProxy(false)86 immerProxy(baseState, draft => {Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
lines 87–90why, one step back87 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {88 draft[i].done = true89 }90 })Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 91–110why, one step back91 setAutoFreezeProxy(true)92 })9394 test("immer (es5) - with autofreeze", () => {95 setAutoFreezeEs5(true)96 immerEs5(frozenBazeState, draft => {97 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {98 draft[i].done = true99 }100 })101 })102103 test("immer (es5) - without autofreeze", () => {104 setAutoFreezeEs5(false)105 immerEs5(baseState, draft => {106 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {107 draft[i].done = true108 }109 })110 setAutoFreezeEs5(true)Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
lines 111–112why, one step back111 })112})