todo.js
This file
Path
__performance_tests__/todo.js
300 lines · 6.7 KiB · JavaScript
Last changed by
Update readmeMichel Weststratebefore adoption341cfeb
History of this fileTake it
The walk stopped after 200 commits: lines older than 670fcff point at it.
Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 1why, one step back1"use strict"Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 2–3why, one step back23import {measure} from "./measure"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 4why, one step back4import produce, {setAutoFreeze, setUseProxies} from "../dist/immer.umd.js"Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 5–6why, one step back5import cloneDeep from "lodash.clonedeep"6import {List, Record} from "immutable"Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
line 7why, one step back7import Seamless from "seamless-immutable"Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 8–14why, one step back8import deepFreeze from "deep-freeze"910function freeze(x) {11 Object.freeze(x)12 return x13}14Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 15–20why, one step back15const MAX = 5000016const MODIFY_FACTOR = 0.117const baseState = []18let frozenBazeState19let immutableJsBaseState20let seamlessBaseStateMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 21why, one step back21Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 22–25why, one step back22// produce the base state23for (let i = 0; i < MAX; i++) {24 baseState.push({25 todo: "todo_" + i,Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 26why, one step back26 done: false,Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
line 27why, one step back27 someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 28why, one step back28 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
line 29why, one step back29}Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 30why, one step back30Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 31–32why, one step back31// Produce the frozen bazeState32frozenBazeState = deepFreeze(cloneDeep(baseState))Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 33why, one step back33Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 34–40why, one step back34// generate immutalbeJS base state35const todoRecord = Record({36 todo: "",37 done: false,38 someThingCompletelyIrrelevant: []39})40immutableJsBaseState = List(baseState.map(todo => todoRecord(todo)))Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 41why, one step back41Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 42–43why, one step back42// generate seamless-immutable base state43seamlessBaseState = Seamless.from(baseState)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 44why, one step back44Update readmeMichel Weststratebefore adoption341cfeb
line 45why, one step back45console.log("\n# todo - performance\n")Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 46–50why, one step back4647measure(48 "just mutate",49 () => ({draft: cloneDeep(baseState)}),50 ({draft}) => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 51–53why, one step back51 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {52 draft[i].done = true53 }Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 54–55why, one step back54 }55)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 56why, one step back56Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 57–60why, one step back57measure(58 "just mutate, freeze",59 () => ({draft: cloneDeep(baseState)}),60 ({draft}) => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 61–64why, one step back61 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {62 draft[i].done = true63 }64 deepFreeze(draft)Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 65–66why, one step back65 }66)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 67why, one step back67Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 68–96why, one step back68measure("deepclone, then mutate", () => {69 const draft = cloneDeep(baseState)70 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {71 draft[i].done = true72 }73})7475measure("deepclone, then mutate, then freeze", () => {76 const draft = cloneDeep(baseState)77 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {78 draft[i].done = true79 }80 deepFreeze(draft)81})8283measure("handcrafted reducer (no freeze)", () => {84 const nextState = [85 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo => ({86 ...todo,87 done: true88 })),89 ...baseState.slice(MAX * MODIFY_FACTOR)90 ]91})9293measure("handcrafted reducer (with freeze)", () => {94 const nextState = freeze([95 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo =>96 freeze({Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 97–98why, one step back97 ...todo,98 done: trueMerge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 99–103why, one step back99 })100 ),101 ...baseState.slice(MAX * MODIFY_FACTOR)102 ])103})Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 104why, one step back104Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 105–112why, one step back105measure("naive handcrafted reducer (without freeze)", () => {106 const nextState = baseState.map((todo, index) => {107 if (index < MAX * MODIFY_FACTOR)108 return {109 ...todo,110 done: true111 }112 else return todoMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 113why, one step back113 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
line 114why, one step back114})Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 115why, one step back115Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 116–118why, one step back116measure("naive handcrafted reducer (with freeze)", () => {117 const nextState = deepFreeze(118 baseState.map((todo, index) => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 119–125why, one step back119 if (index < MAX * MODIFY_FACTOR)120 return {121 ...todo,122 done: true123 }124 else return todo125 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 126–127why, one step back126 )127})Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 128why, one step back128Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 129–134why, one step back129measure("immutableJS", () => {130 let state = immutableJsBaseState131 state.withMutations(state => {132 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {133 state.setIn([i, "done"], true)134 }Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 135why, one step back135 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
line 136why, one step back136})Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 137why, one step back137Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 138–140why, one step back138measure("immutableJS + toJS", () => {139 let state = immutableJsBaseState140 .withMutations(state => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 141–144why, one step back141 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {142 state.setIn([i, "done"], true)143 }144 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 145–146why, one step back145 .toJS()146})Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 147why, one step back147Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 148–152why, one step back148measure("seamless-immutable", () => {149 const state = seamlessBaseState150 state.map((todo, index) => {151 if (index < MAX * MODIFY_FACTOR) return todo.set("done", true)152 else return todoMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 153why, one step back153 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
line 154why, one step back154})Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 155why, one step back155Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 156–168why, one step back156measure("seamless-immutable + asMutable", () => {157 const state = seamlessBaseState158 state159 .map((todo, index) => {160 if (index < MAX * MODIFY_FACTOR) return todo.set("done", true)161 else return todo162 })163 .asMutable({deep: true})164})165166measure(167 "immer (proxy) - without autofreeze",168 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 169–170why, one step back169 setUseProxies(true)170 setAutoFreeze(false)Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 171–172why, one step back171 },172 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 173why, one step back173 produce(baseState, draft => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 174–177why, one step back174 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {175 draft[i].done = true176 }177 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 178–179why, one step back178 }179)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 180why, one step back180Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 181–183why, one step back181measure(182 "immer (proxy) - with autofreeze",183 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 184–185why, one step back184 setUseProxies(true)185 setAutoFreeze(true)Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 186–187why, one step back186 },187 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 188why, one step back188 produce(frozenBazeState, draft => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 189–192why, one step back189 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {190 draft[i].done = true191 }192 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 193–194why, one step back193 }194)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 195why, one step back195Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
line 196why, one step back196measure(Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 197–215why, one step back197 "immer (proxy) - without autofreeze - with patch listener",198 () => {199 setUseProxies(true)200 setAutoFreeze(false)201 },202 () => {203 produce(204 baseState,205 draft => {206 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {207 draft[i].done = true208 }209 },210 function() {}211 )212 }213)214215measure(Update readmeMichel Weststratebefore adoption341cfeb
lines 216–234why, one step back216 "immer (proxy) - with autofreeze - with patch listener",217 () => {218 setUseProxies(true)219 setAutoFreeze(true)220 },221 () => {222 produce(223 baseState,224 draft => {225 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {226 draft[i].done = true227 }228 },229 function() {}230 )231 }232)233234measure(Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 235–236why, one step back235 "immer (es5) - without autofreeze",236 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 237–238why, one step back237 setUseProxies(false)238 setAutoFreeze(false)Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 239–240why, one step back239 },240 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 241why, one step back241 produce(baseState, draft => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 242–245why, one step back242 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {243 draft[i].done = true244 }245 })Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 246–247why, one step back246 }247)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 248why, one step back248Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 249–251why, one step back249measure(250 "immer (es5) - with autofreeze",251 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 252–253why, one step back252 setUseProxies(false)253 setAutoFreeze(true)Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 254–255why, one step back254 },255 () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 256why, one step back256 produce(frozenBazeState, draft => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 257–260why, one step back257 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {258 draft[i].done = true259 }260 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 261–268why, one step back261 }262)263264measure(265 "immer (es5) - without autofreeze - with patch listener",266 () => {267 setUseProxies(false)268 setAutoFreeze(false)Update readmeMichel Weststratebefore adoption341cfeb
lines 269–287why, one step back269 },270 () => {271 produce(272 baseState,273 draft => {274 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {275 draft[i].done = true276 }277 },278 function() {}279 )280 }281)282283measure(284 "immer (es5) - with autofreeze - with patch listener",285 () => {286 setUseProxies(false)287 setAutoFreeze(true)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 288–298why, one step back288 },289 () => {290 produce(291 baseState,292 draft => {293 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {294 draft[i].done = true295 }296 },297 function() {}298 )Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240
lines 299–300why, one step back299 }300)