Skip to content

Commit

Permalink
Add benchmark for setUseStrictShallowCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed May 19, 2022
1 parent d693e8f commit f9cdf8e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
62 changes: 62 additions & 0 deletions __performance_tests__/large-obj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {measure} from "./measure"
import produce, {
setUseProxies,
setUseStrictShallowCopy,
enableAllPlugins
} from "../dist/immer.cjs.production.min.js"

enableAllPlugins()

console.log("\n# large-obj - mutate large object\n")

const MAX = 50

const baseState = Object.fromEntries(
Array(10000)
.fill(0)
.map((_, i) => [i, i])
)

measure("immer (proxy) - with setUseStrictShallowCopy", () => {
setUseStrictShallowCopy(true)
setUseProxies(true)

for (let i = 0; i < MAX; i++) {
produce(baseState, draft => {
draft[5000]++
})
}
})

measure("immer (proxy) - without setUseStrictShallowCopy", () => {
setUseStrictShallowCopy(false)
setUseProxies(true)

for (let i = 0; i < MAX; i++) {
produce(baseState, draft => {
draft[5000]++
})
}
})

measure("immer (es5) - with setUseStrictShallowCopy", () => {
setUseStrictShallowCopy(true)
setUseProxies(false)

for (let i = 0; i < MAX; i++) {
produce(baseState, draft => {
draft[5000]++
})
}
})

measure("immer (es5) - without setUseStrictShallowCopy", () => {
setUseStrictShallowCopy(false)
setUseProxies(false)

for (let i = 0; i < MAX; i++) {
produce(baseState, draft => {
draft[5000]++
})
}
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"sideEffects": false,
"scripts": {
"test": "jest && yarn test:build && yarn test:flow",
"test:perf": "cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",
"test:perf": "cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js && babel-node large-obj.js",
"test:flow": "yarn flow check __tests__/flow",
"test:build": "yarn build && NODE_ENV='production' yarn jest --config jest.config.build.js",
"watch": "jest --watch",
Expand Down

0 comments on commit f9cdf8e

Please sign in to comment.