Skip to content
Myr

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 file

Take it

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

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

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

line 2why, one step back
2import 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 back
3import cloneDeep from "lodash.clonedeep"
4import {List, Record} from "immutable"

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

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

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

line 6why, one step back
6

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

lines 7–11why, one step back
7function freeze(x) {
8 Object.freeze(x)9 return x10}11

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

lines 12–28why, one step back
12describe("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 bazeState

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

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

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

lines 30–43why, one step back
30
31 // 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 }43

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

lines 44–64why, one step back
44 {
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 back
65 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {

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

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

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

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

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

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

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

lines 71–74why, one step back
71 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 back
75 deepFreeze(draft)

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

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

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

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

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

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

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

lines 88–123why, one step back
88 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 })123

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

lines 124–132why, one step back
124 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 })132

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

lines 133–140why, one step back
133 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 back
141 })
142143 measure("immer (proxy) - without autofreeze", () => {

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

lines 144–146why, one step back
144 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 back
147 for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
148 draft[i].done = true149 }150 })151 })152

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

line 153why, one step back
153 measure("immer (proxy) - with autofreeze", () => {

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

lines 154–156why, one step back
154 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 back
157 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 back
164 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 back
167 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 back
171 })
172173 measure("immer (es5) - with autofreeze", () => {

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

lines 174–176why, one step back
174 setUseProxies(false)
175 setAutoFreeze(true)176 produce(frozenBazeState, draft => {

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

lines 177–180why, one step back
177 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 back
181 })
182})