Skip to content
Myr

todo.js

This file

Path

__performance_tests__/todo.js

181 lines · 5.0 KiB · JavaScript

Last changed by

Default build includes both Proxy and ES5 implementationMichel Weststratebefore adoptionbd9c47b

History of this file

Take it

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

line 1why, one step back
1"use strict"

Default build includes both Proxy and ES5 implementationMichel Weststratebefore adoptionbd9c47b

line 2why, one step back
2import produceProxy, {setAutoFreeze as setAutoFreezeProxy} from "../proxy"

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 3why, one step back
3import produceEs5, {setAutoFreeze as setAutoFreezeEs5} from "../es5"

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

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

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

line 6why, one step back
6import deepFreeze from "deep-freeze"

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

line 7why, one step back
7

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

lines 8–12why, one step back
8function freeze(x) {
9 Object.freeze(x)10 return x11}12

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 13–29why, one step back
13describe("performance", () => {
14 const MAX = 10000015 const MODIFY_FACTOR = 0.116 const baseState = []17 let frozenBazeState18 let immutableJsBaseState1920 // produce the base state21 for (let i = 0; i < MAX; i++) {22 baseState.push({23 todo: "todo_" + i,24 done: false,25 someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]26 })27 }2829 // Produce the frozen bazeState

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

line 30why, one step back
30 frozenBazeState = deepFreeze(cloneDeep(baseState))

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 31–44why, one step back
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)))3940 function measure(name, fn) {41 global.gc && global.gc()42 test(name, fn)43 }44

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

lines 45–65why, one step back
45 {
46 const draft = cloneDeep(baseState)47 measure("just mutate", () => {48 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {49 draft[i].done = true50 }51 })52 }5354 {55 const draft = cloneDeep(baseState)56 measure("just mutate, freeze", () => {57 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {58 draft[i].done = true59 }60 deepFreeze(draft)61 })62 }6364 measure("deepclone, then mutate", () => {65 const draft = cloneDeep(baseState)

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

line 66why, one step back
66 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {

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

line 67why, one step back
67 draft[i].done = true

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 68–70why, one step back
68 }
69 })70

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

line 71why, one step back
71 measure("deepclone, then mutate, then freeze", () => {

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 72–75why, one step back
72 const draft = cloneDeep(baseState)
73 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {74 draft[i].done = true75 }

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

line 76why, one step back
76 deepFreeze(draft)

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 77–78why, one step back
77 })
78

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

line 79why, one step back
79 measure("handcrafted reducer (no freeze)", () => {

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 80–88why, one step back
80 const nextState = [
81 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo => ({82 ...todo,83 done: true84 })),85 ...baseState.slice(MAX * MODIFY_FACTOR)86 ]87 })88

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

lines 89–124why, one step back
89 measure("handcrafted reducer (with freeze)", () => {
90 const nextState = freeze([91 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo =>92 freeze({93 ...todo,94 done: true95 })96 ),97 ...baseState.slice(MAX * MODIFY_FACTOR)98 ])99 })100101 measure("naive handcrafted reducer (without freeze)", () => {102 const nextState = baseState.map((todo, index) => {103 if (index < MAX * MODIFY_FACTOR)104 return {105 ...todo,106 done: true107 }108 else return todo109 })110 })111112 measure("naive handcrafted reducer (with freeze)", () => {113 const nextState = deepFreeze(114 baseState.map((todo, index) => {115 if (index < MAX * MODIFY_FACTOR)116 return {117 ...todo,118 done: true119 }120 else return todo121 })122 )123 })124

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 125–133why, one step back
125 measure("immutableJS", () => {
126 let state = immutableJsBaseState127 state.withMutations(state => {128 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {129 state.setIn([i, "done"], true)130 }131 })132 })133

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

lines 134–141why, one step back
134 measure("immutableJS + toJS", () => {
135 let state = immutableJsBaseState136 .withMutations(state => {137 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {138 state.setIn([i, "done"], true)139 }140 })141 .toJS()

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 142–145why, one step back
142 })
143144 measure("immer (proxy) - without autofreeze", () => {145 setAutoFreezeProxy(false)

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 146why, one step back
146 produceProxy(baseState, draft => {

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 147–153why, one step back
147 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
148 draft[i].done = true149 }150 })151 setAutoFreezeProxy(true)152 })153

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

lines 154–155why, one step back
154 measure("immer (proxy) - with autofreeze", () => {
155 setAutoFreezeProxy(true)

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 156why, one step back
156 produceProxy(frozenBazeState, draft => {

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 157–164why, one step back
157 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
158 draft[i].done = true159 }160 })161 })162163 measure("immer (es5) - without autofreeze", () => {164 setAutoFreezeEs5(false)

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 165why, one step back
165 produceEs5(baseState, draft => {

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 166–170why, one step back
166 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
167 draft[i].done = true168 }169 })170 setAutoFreezeEs5(true)

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

lines 171–174why, one step back
171 })
172173 measure("immer (es5) - with autofreeze", () => {174 setAutoFreezeEs5(true)

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 175why, one step back
175 produceEs5(frozenBazeState, draft => {

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

lines 176–179why, one step back
176 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
177 draft[i].done = true178 }179 })

use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65

lines 180–181why, one step back
180 })
181})