todo.js
This file
Path
__performance_tests__/todo.js
182 lines · 4.9 KiB · JavaScript
Last changed by
Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
History of this fileTake it
use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
line 1why, one step back1"use strict"Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 2why, one step back2import produce, {setAutoFreeze, setUseProxies} from "../dist/immer.umd.js"use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 3–4why, one step back3import cloneDeep from "lodash.clonedeep"4import {List, Record} from "immutable"Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 5why, one step back5import deepFreeze from "deep-freeze"use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
line 6why, one step back6Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 7–11why, one step back7function freeze(x) {8 Object.freeze(x)9 return x10}11use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 12–28why, one step back12describe("performance", () => {13 const MAX = 10000014 const MODIFY_FACTOR = 0.115 const baseState = []16 let frozenBazeState17 let immutableJsBaseState1819 // produce the base state20 for (let i = 0; i < MAX; i++) {21 baseState.push({22 todo: "todo_" + i,23 done: false,24 someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]25 })26 }2728 // Produce the frozen bazeStateMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 29why, one step back29 frozenBazeState = deepFreeze(cloneDeep(baseState))use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 30–43why, one step back3031 // generate immutalbeJS base state32 const todoRecord = Record({33 todo: "",34 done: false,35 someThingCompletelyIrrelevant: []36 })37 immutableJsBaseState = List(baseState.map(todo => todoRecord(todo)))3839 function measure(name, fn) {40 global.gc && global.gc()41 test(name, fn)42 }43Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 44–64why, one step back44 {45 const draft = cloneDeep(baseState)46 measure("just mutate", () => {47 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {48 draft[i].done = true49 }50 })51 }5253 {54 const draft = cloneDeep(baseState)55 measure("just mutate, freeze", () => {56 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {57 draft[i].done = true58 }59 deepFreeze(draft)60 })61 }6263 measure("deepclone, then mutate", () => {64 const draft = cloneDeep(baseState)use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
line 65why, one step back65 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 66why, one step back66 draft[i].done = trueuse garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 67–69why, one step back67 }68 })69Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 70why, one step back70 measure("deepclone, then mutate, then freeze", () => {use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 71–74why, one step back71 const draft = cloneDeep(baseState)72 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {73 draft[i].done = true74 }Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 75why, one step back75 deepFreeze(draft)use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 76–77why, one step back76 })77Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 78why, one step back78 measure("handcrafted reducer (no freeze)", () => {use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 79–87why, one step back79 const nextState = [80 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo => ({81 ...todo,82 done: true83 })),84 ...baseState.slice(MAX * MODIFY_FACTOR)85 ]86 })87Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 88–123why, one step back88 measure("handcrafted reducer (with freeze)", () => {89 const nextState = freeze([90 ...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo =>91 freeze({92 ...todo,93 done: true94 })95 ),96 ...baseState.slice(MAX * MODIFY_FACTOR)97 ])98 })99100 measure("naive handcrafted reducer (without freeze)", () => {101 const nextState = baseState.map((todo, index) => {102 if (index < MAX * MODIFY_FACTOR)103 return {104 ...todo,105 done: true106 }107 else return todo108 })109 })110111 measure("naive handcrafted reducer (with freeze)", () => {112 const nextState = deepFreeze(113 baseState.map((todo, index) => {114 if (index < MAX * MODIFY_FACTOR)115 return {116 ...todo,117 done: true118 }119 else return todo120 })121 )122 })123use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 124–132why, one step back124 measure("immutableJS", () => {125 let state = immutableJsBaseState126 state.withMutations(state => {127 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {128 state.setIn([i, "done"], true)129 }130 })131 })132Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 133–140why, one step back133 measure("immutableJS + toJS", () => {134 let state = immutableJsBaseState135 .withMutations(state => {136 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {137 state.setIn([i, "done"], true)138 }139 })140 .toJS()use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 141–143why, one step back141 })142143 measure("immer (proxy) - without autofreeze", () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 144–146why, one step back144 setUseProxies(true)145 setAutoFreeze(false)146 produce(baseState, draft => {use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 147–152why, one step back147 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {148 draft[i].done = true149 }150 })151 })152Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 153why, one step back153 measure("immer (proxy) - with autofreeze", () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 154–156why, one step back154 setUseProxies(true)155 setAutoFreeze(true)156 produce(frozenBazeState, draft => {use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 157–163why, one step back157 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {158 draft[i].done = true159 }160 })161 })162163 measure("immer (es5) - without autofreeze", () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 164–166why, one step back164 setUseProxies(false)165 setAutoFreeze(false)166 produce(baseState, draft => {use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 167–170why, one step back167 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {168 draft[i].done = true169 }170 })Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 171–173why, one step back171 })172173 measure("immer (es5) - with autofreeze", () => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 174–176why, one step back174 setUseProxies(false)175 setAutoFreeze(true)176 produce(frozenBazeState, draft => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 177–180why, one step back177 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {178 draft[i].done = true179 }180 })use garbage collector to make test setup more reliable, fixes #27Michel Weststratebefore adoptionc3fcf65
lines 181–182why, one step back181 })182})