Skip to content
Myr

incremental.js

This file

Path

__performance_tests__/incremental.js

135 lines · 2.7 KiB · JavaScript

Last changed by

Update readmeMichel Weststratebefore adoption341cfeb

History of this file

Take it

Merge branch 'scriby-master'Michel Weststratebefore adoption3d3732c

lines 1–6why, one step back
1"use strict"
2import {measure} from "./measure"3import produce, {setAutoFreeze, setUseProxies} from "../dist/immer.umd.js"4import cloneDeep from "lodash.clonedeep"5import * as Immutable from "immutable"6

Update readmeMichel Weststratebefore adoption341cfeb

lines 7–8why, one step back
7console.log("\n# incremental - lot of small incremental changes\n")
8

Merge branch 'scriby-master'Michel Weststratebefore adoption3d3732c

lines 9–15why, one step back
9function createTestObject() {
10 return {11 a: 1,12 b: "Some data here"13 }14}15

Increased number a bit; show the importance of singel produceMichel Weststratebefore adoption2d4590e

line 16why, one step back
16const MAX = 1000

Merge branch 'scriby-master'Michel Weststratebefore adoption3d3732c

lines 17–40why, one step back
17const baseState = {
18 ids: [],19 map: Object.create(null)20}2122let immutableJsBaseState2324immutableJsBaseState = {25 ids: Immutable.List(),26 map: Immutable.Map()27}2829measure(30 "just mutate",31 () => cloneDeep(baseState),32 draft => {33 for (let i = 0; i < MAX; i++) {34 draft.ids.push(i)35 draft.map[i] = createTestObject()36 }37 }38)3940measure(

Increased number a bit; show the importance of singel produceMichel Weststratebefore adoption2d4590e

line 41why, one step back
41 "handcrafted reducer",

Merge branch 'scriby-master'Michel Weststratebefore adoption3d3732c

lines 42–69why, one step back
42 () => cloneDeep(baseState),
43 state => {44 for (let i = 0; i < MAX; i++) {45 state = {46 ids: [...state.ids, i],47 map: {48 ...state.map,49 [i]: createTestObject()50 }51 }52 }53 }54)5556measure(57 "immutableJS",58 () => immutableJsBaseState,59 state => {60 for (let i = 0; i < MAX; i++) {61 state = {62 ids: state.ids.push(i),63 map: state.map.set(i, createTestObject())64 }65 }66 }67)6869measure(

Increased number a bit; show the importance of singel produceMichel Weststratebefore adoption2d4590e

line 70why, one step back
70 "immer (proxy)",

Merge branch 'scriby-master'Michel Weststratebefore adoption3d3732c

lines 71–86why, one step back
71 () => {
72 setUseProxies(true)73 setAutoFreeze(false)74 return baseState75 },76 state => {77 for (let i = 0; i < MAX; i++) {78 state = produce(state, draft => {79 draft.ids.push(i)80 draft.map[i] = createTestObject()81 })82 }83 }84)8586measure(

Increased number a bit; show the importance of singel produceMichel Weststratebefore adoption2d4590e

line 87why, one step back
87 "immer (es5)",

Merge branch 'scriby-master'Michel Weststratebefore adoption3d3732c

lines 88–99why, one step back
88 () => {
89 setUseProxies(false)90 setAutoFreeze(false)91 return baseState92 },93 state => {94 for (let i = 0; i < MAX; i++) {95 state = produce(state, draft => {96 draft.ids.push(i)97 draft.map[i] = createTestObject()98 })99 }

Increased number a bit; show the importance of singel produceMichel Weststratebefore adoption2d4590e

lines 100–133why, one step back
100 }
101)102103measure(104 "immer (proxy) - single produce",105 () => {106 setUseProxies(true)107 setAutoFreeze(false)108 return baseState109 },110 state => {111 produce(state, draft => {112 for (let i = 0; i < MAX; i++) {113 draft.ids.push(i)114 draft.map[i] = createTestObject()115 }116 })117 }118)119120measure(121 "immer (es5) - single produce",122 () => {123 setUseProxies(false)124 setAutoFreeze(false)125 return baseState126 },127 state => {128 produce(state, draft => {129 for (let i = 0; i < MAX; i++) {130 draft.ids.push(i)131 draft.map[i] = createTestObject()132 }133 })

Merge branch 'scriby-master'Michel Weststratebefore adoption3d3732c

lines 134–135why, one step back
134 }
135)