Skip to content
Myr

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 file

Take 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 back
1"use strict"

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 2–3why, one step back
2
3import {measure} from "./measure"

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 4why, one step back
4import produce, {setAutoFreeze, setUseProxies} from "../dist/immer.umd.js"

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

lines 5–6why, one step back
5import cloneDeep from "lodash.clonedeep"
6import {List, Record} from "immutable"

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

line 7why, one step back
7import Seamless from "seamless-immutable"

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

lines 8–14why, one step back
8import deepFreeze from "deep-freeze"
910function freeze(x) {11 Object.freeze(x)12 return x13}14

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 15–20why, one step back
15const MAX = 50000
16const MODIFY_FACTOR = 0.117const baseState = []18let frozenBazeState19let immutableJsBaseState20let seamlessBaseState

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

line 21why, one step back
21

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 22–25why, one step back
22// produce the base state
23for (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 back
26 done: false,

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

line 27why, one step back
27 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 back
28 })

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

line 29why, one step back
29}

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

line 30why, one step back
30

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 31–32why, one step back
31// Produce the frozen bazeState
32frozenBazeState = deepFreeze(cloneDeep(baseState))

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

line 33why, one step back
33

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 34–40why, one step back
34// generate immutalbeJS base state
35const 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 back
41

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 42–43why, one step back
42// generate seamless-immutable base state
43seamlessBaseState = Seamless.from(baseState)

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

line 44why, one step back
44

Update readmeMichel Weststratebefore adoption341cfeb

line 45why, one step back
45console.log("\n# todo - performance\n")

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 46–50why, one step back
46
47measure(48 "just mutate",49 () => ({draft: cloneDeep(baseState)}),50 ({draft}) => {

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

lines 51–53why, one step back
51 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 back
54 }
55)

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

line 56why, one step back
56

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 57–60why, one step back
57measure(
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 back
61 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 back
65 }
66)

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

line 67why, one step back
67

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 68–96why, one step back
68measure("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 back
97 ...todo,
98 done: true

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 99–103why, one step back
99 })
100 ),101 ...baseState.slice(MAX * MODIFY_FACTOR)102 ])103})

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

line 104why, one step back
104

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 105–112why, one step back
105measure("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 todo

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

line 113why, one step back
113 })

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

line 114why, one step back
114})

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

line 115why, one step back
115

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 116–118why, one step back
116measure("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 back
119 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 back
126 )
127})

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

line 128why, one step back
128

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 129–134why, one step back
129measure("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 back
135 })

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

line 136why, one step back
136})

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

line 137why, one step back
137

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 138–140why, one step back
138measure("immutableJS + toJS", () => {
139 let state = immutableJsBaseState140 .withMutations(state => {

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

lines 141–144why, one step back
141 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 back
145 .toJS()
146})

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

line 147why, one step back
147

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 148–152why, one step back
148measure("seamless-immutable", () => {
149 const state = seamlessBaseState150 state.map((todo, index) => {151 if (index < MAX * MODIFY_FACTOR) return todo.set("done", true)152 else return todo

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

line 153why, one step back
153 })

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

line 154why, one step back
154})

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

line 155why, one step back
155

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 156–168why, one step back
156measure("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 back
169 setUseProxies(true)
170 setAutoFreeze(false)

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 171–172why, one step back
171 },
172 () => {

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

line 173why, one step back
173 produce(baseState, draft => {

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

lines 174–177why, one step back
174 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 back
178 }
179)

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

line 180why, one step back
180

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 181–183why, one step back
181measure(
182 "immer (proxy) - with autofreeze",183 () => {

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

lines 184–185why, one step back
184 setUseProxies(true)
185 setAutoFreeze(true)

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 186–187why, one step back
186 },
187 () => {

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

line 188why, one step back
188 produce(frozenBazeState, draft => {

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

lines 189–192why, one step back
189 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 back
193 }
194)

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

line 195why, one step back
195

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

line 196why, one step back
196measure(

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 197–215why, one step back
197 "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 back
216 "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 back
235 "immer (es5) - without autofreeze",
236 () => {

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

lines 237–238why, one step back
237 setUseProxies(false)
238 setAutoFreeze(false)

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 239–240why, one step back
239 },
240 () => {

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

line 241why, one step back
241 produce(baseState, draft => {

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

lines 242–245why, one step back
242 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 back
246 }
247)

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

line 248why, one step back
248

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 249–251why, one step back
249measure(
250 "immer (es5) - with autofreeze",251 () => {

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

lines 252–253why, one step back
252 setUseProxies(false)
253 setAutoFreeze(true)

Merge branch 'pekeler-seamless-immutable'Michel Weststratebefore adoption6e23240

lines 254–255why, one step back
254 },
255 () => {

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

line 256why, one step back
256 produce(frozenBazeState, draft => {

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

lines 257–260why, one step back
257 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 back
261 }
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 back
269 },
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 back
288 },
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 back
299 }
300)