-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.ts
180 lines (169 loc) · 3.92 KB
/
benchmark.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import fs from 'fs';
import https from 'https';
import { Suite } from 'benchmark';
import QuickChart from 'quickchart-js';
import { create as createWithZustand } from 'zustand';
import { immer } from 'zustand/middleware/immer';
import { mutative } from '../src';
const labels: string[] = [];
const result = [
{
label: 'Zustand with Mutative',
backgroundColor: 'rgba(255, 0, 217, 0.5)',
data: [],
},
{
label: 'Zustand with Immer',
backgroundColor: 'rgba(255, 0, 0, 0.5)',
data: [],
},
];
interface Data {
arr: Record<string, number>[];
map: Record<string, Record<string, number>>;
}
type Store = Data & {
update: () => void;
};
const getData = () => {
const baseState: Data = {
arr: [],
map: {},
};
const createTestObject = () =>
Array(10 * 5)
.fill(1)
.reduce((i, _, k) => Object.assign(i, { [k]: k }), {});
baseState.arr = Array(10 ** 4 * 5)
.fill('')
.map(() => createTestObject());
Array(10 ** 3)
.fill(1)
.forEach((_, i) => {
baseState.map[i] = { i };
});
return baseState;
// return deepFreeze(baseState);
};
let baseState: any;
let i: any;
let store: any;
const suite = new Suite();
suite
.add(
'Zustand with Mutative - Update big array and object',
() => {
store.getState().update();
},
{
onStart: () => {
i = Math.random();
baseState = getData();
store = createWithZustand(
mutative<Store>((set) => ({
arr: baseState.arr,
map: baseState.map,
update: () => {
set((state) => {
state.arr.push(i);
state.map[i] = { i };
});
},
}))
);
},
}
)
.add(
'Zustand with Immer - Update big array and object',
() => {
store.getState().update();
},
{
onStart: () => {
i = Math.random();
baseState = getData();
store = createWithZustand(
immer<Store>((set) => ({
arr: baseState.arr,
map: baseState.map,
update: () => {
set((state) => {
state.arr.push(i);
state.map[i] = { i };
});
},
}))
);
},
}
)
.on('cycle', (event: any) => {
console.log(String(event.target));
const [name, field = 'Update'] = event.target.name.split(' - ');
if (!labels.includes(field)) labels.push(field);
const item = result.find(({ label }) => label === name);
// @ts-ignore
item.data[labels.indexOf(field)] = Math.round(event.target.hz);
})
.on('complete', function (this: any) {
console.log(`The fastest method is ${this.filter('fastest').map('name')}`);
})
.run({ async: false });
try {
const config = {
type: 'horizontalBar',
data: {
labels,
datasets: result,
},
options: {
title: {
display: true,
text: 'Zustand with Mutative vs Zustand with Immer - Performance',
},
legend: {
position: 'bottom',
},
elements: {
rectangle: {
borderWidth: 1,
},
},
scales: {
xAxes: [
{
display: true,
scaleLabel: {
display: true,
fontSize: 10,
labelString:
'Measure(ops/sec) to update 50K arrays and 1K objects, bigger is better.',
},
},
],
},
plugins: {
datalabels: {
anchor: 'center',
align: 'center',
font: {
size: 8,
},
},
},
},
};
const chart = new QuickChart();
chart.setConfig(config);
const file = fs.createWriteStream('benchmark.jpg');
https.get(chart.getUrl(), (response) => {
response.pipe(file);
file.on('finish', () => {
file.close();
console.log('update benchmark');
});
});
} catch (err) {
console.error(err);
}