-
-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c727911
commit 0b9bc8d
Showing
19 changed files
with
1,350 additions
and
706 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
repo_token: EcsPD2u7ovNdmEGttaSGFaXqetDUAbnZ3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
env: | ||
- NODE_ENV=TEST | ||
cache: | ||
directories: | ||
- "node_modules" | ||
script: yarn coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
"use strict" | ||
import immerProxy, {setAutoFreeze as setAutoFreezeProxy} from ".." | ||
import immerEs5, {setAutoFreeze as setAutoFreezeEs5} from "../es5" | ||
import cloneDeep from "lodash.clonedeep" | ||
import {List, Record} from "immutable" | ||
import deepFreeze from "deep-freeze" | ||
|
||
function freeze(x) { | ||
Object.freeze(x) | ||
return x | ||
} | ||
|
||
describe("performance", () => { | ||
const MAX = 100000 | ||
const MODIFY_FACTOR = 0.1 | ||
const baseState = [] | ||
let frozenBazeState | ||
let immutableJsBaseState | ||
|
||
// produce the base state | ||
for (let i = 0; i < MAX; i++) { | ||
baseState.push({ | ||
todo: "todo_" + i, | ||
done: false, | ||
someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] | ||
}) | ||
} | ||
|
||
// Produce the frozen bazeState | ||
frozenBazeState = deepFreeze(cloneDeep(baseState)) | ||
|
||
// generate immutalbeJS base state | ||
const todoRecord = Record({ | ||
todo: "", | ||
done: false, | ||
someThingCompletelyIrrelevant: [] | ||
}) | ||
immutableJsBaseState = List(baseState.map(todo => todoRecord(todo))) | ||
|
||
function measure(name, fn) { | ||
global.gc && global.gc() | ||
test(name, fn) | ||
} | ||
|
||
{ | ||
const draft = cloneDeep(baseState) | ||
measure("just mutate", () => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
}) | ||
} | ||
|
||
{ | ||
const draft = cloneDeep(baseState) | ||
measure("just mutate, freeze", () => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
deepFreeze(draft) | ||
}) | ||
} | ||
|
||
measure("deepclone, then mutate", () => { | ||
const draft = cloneDeep(baseState) | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
}) | ||
|
||
measure("deepclone, then mutate, then freeze", () => { | ||
const draft = cloneDeep(baseState) | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
deepFreeze(draft) | ||
}) | ||
|
||
measure("handcrafted reducer (no freeze)", () => { | ||
const nextState = [ | ||
...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo => ({ | ||
...todo, | ||
done: true | ||
})), | ||
...baseState.slice(MAX * MODIFY_FACTOR) | ||
] | ||
}) | ||
|
||
measure("handcrafted reducer (with freeze)", () => { | ||
const nextState = freeze([ | ||
...baseState.slice(0, MAX * MODIFY_FACTOR).map(todo => | ||
freeze({ | ||
...todo, | ||
done: true | ||
}) | ||
), | ||
...baseState.slice(MAX * MODIFY_FACTOR) | ||
]) | ||
}) | ||
|
||
measure("naive handcrafted reducer (without freeze)", () => { | ||
const nextState = baseState.map((todo, index) => { | ||
if (index < MAX * MODIFY_FACTOR) | ||
return { | ||
...todo, | ||
done: true | ||
} | ||
else return todo | ||
}) | ||
}) | ||
|
||
measure("naive handcrafted reducer (with freeze)", () => { | ||
const nextState = deepFreeze( | ||
baseState.map((todo, index) => { | ||
if (index < MAX * MODIFY_FACTOR) | ||
return { | ||
...todo, | ||
done: true | ||
} | ||
else return todo | ||
}) | ||
) | ||
}) | ||
|
||
measure("immutableJS", () => { | ||
let state = immutableJsBaseState | ||
state.withMutations(state => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
state.setIn([i, "done"], true) | ||
} | ||
}) | ||
}) | ||
|
||
measure("immutableJS + toJS", () => { | ||
let state = immutableJsBaseState | ||
.withMutations(state => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
state.setIn([i, "done"], true) | ||
} | ||
}) | ||
.toJS() | ||
}) | ||
|
||
measure("immer (proxy) - without autofreeze", () => { | ||
setAutoFreezeProxy(false) | ||
immerProxy(baseState, draft => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
}) | ||
setAutoFreezeProxy(true) | ||
}) | ||
|
||
measure("immer (proxy) - with autofreeze", () => { | ||
setAutoFreezeProxy(true) | ||
immerProxy(frozenBazeState, draft => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
}) | ||
}) | ||
|
||
measure("immer (es5) - without autofreeze", () => { | ||
setAutoFreezeEs5(false) | ||
immerEs5(baseState, draft => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
}) | ||
setAutoFreezeEs5(true) | ||
}) | ||
|
||
measure("immer (es5) - with autofreeze", () => { | ||
setAutoFreezeEs5(true) | ||
immerEs5(frozenBazeState, draft => { | ||
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { | ||
draft[i].done = true | ||
} | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.