Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit c6bea27

Browse files
frinyvonnicknlepage
authored andcommitted
📝 Benchmark immutadot and display results on front page (#196)
* ✨ Add the simple benchmark made for immer in a sperate package * 📝 Add benchmark results in README.md * ✏️ Add link to benchmark author github profile * 🚧 Add lodash/fp in benchmark * 🔥 Remove test of immer with autofreeze * 🐛 Reorder args for lodash/fp test * 🔥 Remove lodash/fp test * 📝 Update benchmark results * ♻️ Adjustments in benchmarks * 👌 Take care of @nlepage review * ✏️ Update benchmark results * 📝 Update benchmark presentation * ✏️ fix typo
1 parent 0c48bb3 commit c6bea27

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,21 @@ toLowerCase({ nested: { prop: { 1: { msg: 'Hello' }, 2: { msg: 'Hi' }, 3: { msg
186186
// { nested: { prop: { 1: { msg: 'Hello' }, 2: { msg: 'hi' }, 3: { msg: 'good morning' } } } }
187187
```
188188

189-
### Performances
189+
## Performances
190+
191+
We reused a [simple benchmark](https://github.com/Zenika/immutadot/tree/master/packages/immutadot-benchmark/src/benchmark.spec.js) originally made by [mweststrate](https://github.com/mweststrate) for [immer](https://github.com/mweststrate/immer).
192+
It updates 10.000 items out of list of 100.000 todos items, these tests were executed on Node 8.4.0 with a MacBook Pro 2015:
193+
194+
```shell
195+
Update todos list
196+
✓ with mutation (2ms)
197+
✓ with deep cloning, then mutation (628ms)
198+
✓ with ES2015 destructuring (46ms)
199+
✓ with immutable (w/o conversion to plain JS objects) (99ms)
200+
✓ with immutable (w/ conversion to plain JS objects) (858ms)
201+
✓ with immer (353ms)
202+
✓ with immutad●t (137ms)
203+
```
190204

191205
When applying operations on a path immutad●t tries to create the minimum of objects or arrays needed to guarantee your data structure to be immutable.
192206

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "immutadot-benchmark",
3+
"version": "1.0.0-rc.4",
4+
"license": "MIT",
5+
"devDependencies": {
6+
"immer": "~0.2.0",
7+
"immutable": "~3.8.2",
8+
"immutadot": "~1.0.0-rc.4",
9+
"jest": "~21.2.1"
10+
},
11+
"scripts": {
12+
"benchmark": "jest -v -i"
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* eslint-env jest */
2+
'use strict'
3+
4+
// Initial source: https://github.com/mweststrate/immer/blob/2a1da31f8d0408857697c04f6a683fd955ab7a06/__tests__/performance.js
5+
6+
import { List, Record } from 'immutable'
7+
8+
import immer, { setAutoFreeze } from 'immer'
9+
10+
import cloneDeep from 'lodash/cloneDeep'
11+
12+
import { set } from 'immutadot/core'
13+
14+
describe('Benchmarks', () => {
15+
16+
describe('Update todos list', () => {
17+
const MAX = 100000
18+
const MODIFY_FACTOR = 0.1
19+
20+
// Prepare base state
21+
const baseState = []
22+
for (let i = 0; i < MAX; i++) {
23+
baseState.push({
24+
todo: `todo_${i}`,
25+
done: false,
26+
someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
27+
})
28+
}
29+
30+
// Prepare frozen state
31+
const frozenState = Object.freeze(baseState.map(todo => Object.freeze({
32+
...todo,
33+
someThingCompletelyIrrelevant: Object.freeze([...todo.someThingCompletelyIrrelevant]),
34+
})))
35+
36+
// Prepare immutalbe state
37+
const todoRecord = Record({
38+
todo: '',
39+
done: false,
40+
someThingCompletelyIrrelevant: [],
41+
})
42+
const immutableState = List(frozenState.map(todo => todoRecord(todo)))
43+
44+
// Disable immer auto freeze
45+
setAutoFreeze(false)
46+
47+
// Prepare expectd state
48+
const expectedState = []
49+
for (let i = 0; i < MAX; i++) {
50+
expectedState.push({
51+
todo: `todo_${i}`,
52+
done: i < MAX * MODIFY_FACTOR,
53+
someThingCompletelyIrrelevant: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
54+
})
55+
}
56+
57+
it('with mutation', () => {
58+
for (let i = 0; i < MAX * MODIFY_FACTOR; i++)
59+
baseState[i].done = true
60+
})
61+
62+
let deepCloneActual
63+
it('with deep cloning, then mutation', () => {
64+
deepCloneActual = cloneDeep(frozenState)
65+
for (let i = 0; i < MAX * MODIFY_FACTOR; i++)
66+
deepCloneActual[i].done = true
67+
})
68+
69+
let es2015Actual
70+
it('with ES2015 destructuring', () => {
71+
es2015Actual = frozenState
72+
.slice(0, MAX * MODIFY_FACTOR)
73+
.map(todo => ({
74+
...todo,
75+
done: true,
76+
}))
77+
.concat(frozenState.slice(MAX * MODIFY_FACTOR))
78+
})
79+
80+
it('with immutable (w/o conversion to plain JS objects)', () => {
81+
immutableState.withMutations(state => {
82+
for (let i = 0; i < MAX * MODIFY_FACTOR; i++)
83+
state.setIn([i, 'done'], true)
84+
})
85+
})
86+
87+
let immutableActual
88+
it('with immutable (w/ conversion to plain JS objects)', () => {
89+
immutableActual = immutableState.withMutations(state => {
90+
for (let i = 0; i < MAX * MODIFY_FACTOR; i++)
91+
state.setIn([i, 'done'], true)
92+
}).toJS()
93+
})
94+
95+
let immerActual
96+
it('with immer', () => {
97+
immerActual = immer(frozenState, draft => {
98+
for (let i = 0; i < MAX * MODIFY_FACTOR; i++)
99+
draft[i].done = true
100+
})
101+
})
102+
103+
let immutadotActual
104+
it('with immutad●t', () => {
105+
immutadotActual = set(frozenState, `[:${MAX * MODIFY_FACTOR}].done`, true)
106+
})
107+
108+
it('check produced todos lists', () => {
109+
expect(baseState).toEqual(expectedState)
110+
expect(deepCloneActual).toEqual(expectedState)
111+
expect(es2015Actual).toEqual(expectedState)
112+
expect(immutableActual).toEqual(expectedState)
113+
expect(immerActual).toEqual(expectedState)
114+
expect(immutadotActual).toEqual(expectedState)
115+
})
116+
})
117+
})

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,14 @@ ignore@^3.3.3:
26012601
version "3.3.7"
26022602
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021"
26032603

2604+
immer@~0.2.0:
2605+
version "0.2.0"
2606+
resolved "https://registry.yarnpkg.com/immer/-/immer-0.2.0.tgz#a871923e7bfdfaff0796ba3f9a98c3aec665f389"
2607+
2608+
immutable@~3.8.2:
2609+
version "3.8.2"
2610+
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"
2611+
26042612
imurmurhash@^0.1.4:
26052613
version "0.1.4"
26062614
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"

0 commit comments

Comments
 (0)