-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.ts
270 lines (212 loc) · 7.77 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import {SortedCollectionAdapter} from '../src/sortedcollection';
require('source-map-support').install();
import produce, {setAutoFreeze, setUseProxies} from 'immer';
import {MapAdapter} from '../src/map';
import {SortedMapAdapter} from '../src/sortedmap';
import {LruCacheAdapter} from '../src/lrucache';
setUseProxies(true);
setAutoFreeze(false);
declare const global: any;
const WARMUP_ITERATIONS = 1000;
const ITERATIONS = 4000;
type Obj = { order?: number, id?: number|string, data?: string };
function benchmark(label: string, cb: (iterations: number) => void) {
cb(WARMUP_ITERATIONS); // Warm up V8 to let it optimize the code
global.gc();
console.time(label);
cb(ITERATIONS);
console.timeEnd(label);
}
function immerutableMap() {
benchmark('immerutable map (set)', (iterations) => {
const adapter = new MapAdapter<number, Obj>();
let state = { map: adapter.create() };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
adapter.set(draft.map, i, { id: i, data: i.toString() });
});
}
});
}
function immerutableBtree() {
const sortedCollectionAdapter = new SortedCollectionAdapter<Obj>({
orderComparer: (a: Obj, b: Obj) => a.order! - b.order!,
});
benchmark('immerutable sorted collection (insert in increasing order)', (iterations) => {
let state = { btree: sortedCollectionAdapter.create() };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
sortedCollectionAdapter.insert(draft.btree, { data: i.toString(), order: i });
});
}
});
benchmark('immerutable sorted collection (insert in random order)', (iterations) => {
let state = { btree: sortedCollectionAdapter.create() };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
sortedCollectionAdapter.insert(draft.btree, { data: i.toString(), order: Math.random() });
});
}
});
benchmark('immerutable sorted collection (insert in decreasing order)', (iterations) => {
let state = { btree: sortedCollectionAdapter.create() };
for (let i = iterations - 1; i >= 0; i--) {
state = produce(state, (draft: typeof state) => {
sortedCollectionAdapter.insert(draft.btree, { data: i.toString(), order: i });
});
}
});
benchmark(`immerutable sorted collection iteration (inside immer) (size: ${ITERATIONS})`, (() => {
let state = { btree: sortedCollectionAdapter.create() };
for (let i = 0; i < ITERATIONS; i++) {
sortedCollectionAdapter.insert(state.btree, {data: i.toString(), order: i });
}
return () => {
state = produce(state, (draft: typeof state) => {
for (const item of sortedCollectionAdapter.getIterable(draft.btree)) {
}
});
};
})());
benchmark(`immerutable sorted collection iteration (outside immer) (size: ${ITERATIONS})`, (() => {
let state = { btree: sortedCollectionAdapter.create() };
for (let i = 0; i < ITERATIONS; i++) {
sortedCollectionAdapter.insert(state.btree, {data: i.toString(), order: i });
}
return () => {
for (const item of sortedCollectionAdapter.getIterable(state.btree)) {
}
};
})());
}
function immerutableSortedMap() {
const sortedMapAdapter = new SortedMapAdapter<string, Obj, number>({
getOrderingKey: (obj: Obj) => obj.order!,
});
benchmark('immerutable sorted map (insert in increasing order)', (iterations) => {
let state = { sortedMap: sortedMapAdapter.create() };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
sortedMapAdapter.set(draft.sortedMap, i.toString(), { data: i.toString(), order: i });
});
}
});
benchmark('immerutable sorted map (insert in random order)', (iterations) => {
let state = { sortedMap: sortedMapAdapter.create() };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
sortedMapAdapter.set(draft.sortedMap, i.toString(), { data: i.toString(), order: Math.random() });
});
}
});
benchmark('immerutable sorted map (insert in decreasing order)', (iterations) => {
let state = { sortedMap: sortedMapAdapter.create() };
for (let i = iterations - 1; i >= 0; i--) {
state = produce(state, (draft: typeof state) => {
sortedMapAdapter.set(draft.sortedMap, i.toString(), { data: i.toString(), order: i });
});
}
});
benchmark(`immerutable sorted map iteration (inside immer) (size: ${ITERATIONS})`, (() => {
let state = { sortedMap: sortedMapAdapter.create() };
for (let i = 0; i < ITERATIONS; i++) {
sortedMapAdapter.set(state.sortedMap, i.toString(), {data: i.toString(), order: i });
}
return () => {
state = produce(state, (draft: typeof state) => {
for (const item of sortedMapAdapter.getIterable(draft.sortedMap)) {
}
});
};
})());
benchmark(`immerutable sorted map iteration (outside immer) (size: ${ITERATIONS})`, (() => {
let state = { sortedMap: sortedMapAdapter.create() };
for (let i = 0; i < ITERATIONS; i++) {
sortedMapAdapter.set(state.sortedMap, i.toString(), {data: i.toString(), order: i });
}
return () => {
for (const item of sortedMapAdapter.getIterable(state.sortedMap)) {
}
};
})());
}
function immerArray() {
benchmark('immer array (insert in increasing order)', (iterations) => {
let state = { array: [] as Obj[] };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
draft.array.push({ id: i, data: i.toString() });
});
}
});
benchmark('immer array (insert in random order)', (iterations) => {
let state = { array: [] as Obj[] };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
draft.array.splice(Math.floor(Math.random() * i), 0, { id: i, data: i.toString() });
});
}
});
benchmark('immer array (insert in decreasing order)', (iterations) => {
let state = { array: [] as Obj[] };
for (let i = iterations - 1; i >= 0 ; i--) {
state = produce(state, (draft: typeof state) => {
draft.array.unshift({ id: i, data: i.toString() });
});
}
});
benchmark('immer array iteration (inside immer) (size = 4000)', () => {
let state = { array: [] as Obj[] };
for (let i = 0; i < ITERATIONS; i++) {
state.array.push({ id: i, data: i.toString() });
}
return () => {
state = produce(state, (draft: typeof state) => {
for (const item of draft.array) {
}
});
};
});
}
function immerMap() {
benchmark('immer map (set)', (iterations) => {
let state = { map: Object.create(null) };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
draft.map[i] = { id: i, data: i.toString() };
});
}
});
}
function lruCache() {
benchmark(`lru cache (50% capacity)`, (iterations) => {
const lruCache = new LruCacheAdapter(iterations / 2);
let state = { lru: lruCache.create() };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
lruCache.set(draft.lru, i, { data: i });
});
}
});
benchmark(`lru cache (10% capacity)`, (iterations) => {
const lruCache = new LruCacheAdapter(iterations / 10);
let state = { lru: lruCache.create() };
for (let i = 0; i < iterations; i++) {
state = produce(state, (draft: typeof state) => {
lruCache.set(draft.lru, i, { data: i });
});
}
});
}
function divider() {
console.log('-------------------------------------------');
}
immerMap();
immerutableMap();
divider();
immerArray();
immerutableBtree();
divider();
immerutableSortedMap();
divider();
lruCache();