-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdynamic_deps_bench.ts
355 lines (298 loc) · 9.48 KB
/
dynamic_deps_bench.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { forEach } from 'wonka'
import { printLogs, formatLog, POSITION_KEY } from './utils'
async function testAggregateGrowing(count: number, method: 'push' | 'unshift') {
const mol_wire_lib = await import('mol_wire_lib')
const { $mol_wire_atom } = mol_wire_lib.default
const { atom, createCtx } = await import('@reatom/core')
const V4 = await import('../../reatom4/packages/core/build')
const { observable, computed, autorun, configure } = await import('mobx')
configure({ enforceActions: 'never' })
const { act } = await import('@artalar/act')
const molAtoms = [new $mol_wire_atom(`0`, (next: number = 0) => next)]
const reAtoms = [atom(0, `${0}`)]
const V4Atoms = [V4.atom(0, `${0}`)]
const mobxAtoms = [observable.box(0, { name: `${0}` })]
const actAtoms = [act(0)]
const molAtom = new $mol_wire_atom(`sum`, () =>
molAtoms.reduce((sum, atom) => sum + atom.sync(), 0),
)
const reAtom = atom(
(ctx) => reAtoms.reduce((sum, atom) => sum + ctx.spy(atom), 0),
`sum`,
)
const V4Atom = V4.atom(
() => V4Atoms.reduce((sum, atom) => sum + atom(), 0),
`sum`,
)
const mobxAtom = computed(
() => mobxAtoms.reduce((sum, atom) => sum + atom.get(), 0),
{ name: `sum` },
)
const actAtom = act(() => actAtoms.reduce((sum, a) => sum + a(), 0))
const ctx = createCtx()
const V4Root = V4.AsyncContext.Snapshot.createRoot()
ctx.subscribe(reAtom, () => {})
V4.effect(() => V4Atom()).run(V4Root.frame)
molAtom.sync()
autorun(() => mobxAtom.get())
actAtom.subscribe(() => {})
const reatomLogs = new Array<number>()
const V4Logs = new Array<number>()
const molLogs = new Array<number>()
const mobxLogs = new Array<number>()
const actLogs = new Array<number>()
let i = 1
while (i++ < count) {
const startReatom = performance.now()
reAtoms[method](atom(i, `${i}`))
reAtoms.at(-2)!(ctx, i)
reatomLogs.push(performance.now() - startReatom)
const startMol = performance.now()
molAtoms[method](new $mol_wire_atom(`${i}`, (next: number = i) => next))
molAtoms.at(-2)!.put(i)
molAtom.sync()
molLogs.push(performance.now() - startMol)
const startMobx = performance.now()
mobxAtoms[method](observable.box(i, { name: `${i}` }))
mobxAtoms.at(-2)!.set(i)
mobxLogs.push(performance.now() - startMobx)
const startV4 = performance.now()
V4Root.run(() => {
V4Atoms[method](V4.atom(i))
V4Atoms.at(-2)!(i)
V4.notify()
})
V4Logs.push(performance.now() - startV4)
const startAct = performance.now()
actAtoms[method](act(i))
actAtoms.at(-2)!(i)
act.notify()
actLogs.push(performance.now() - startAct)
await new Promise((resolve) => setTimeout(resolve, 0))
}
if (
new Set([
molAtom.sync(),
ctx.get(reAtom),
mobxAtom.get(),
actAtom(),
V4Root.run(V4Atom),
]).size > 1
) {
throw new Error(
'Mismatch: ' +
JSON.stringify({
mol: molAtom.sync(),
reatom: ctx.get(reAtom),
mobx: mobxAtom.get(),
act: actAtom(),
V4: V4Root.run(V4Atom),
}),
)
}
console.log(
`Median of sum calc of reactive nodes in list from 1 to ${count} (with "${method}")`,
)
return printLogs({
reatom: formatLog(reatomLogs),
$mol_wire: formatLog(molLogs),
mobx: formatLog(mobxLogs),
// act: formatLog(actLogs),
V4: formatLog(V4Logs),
})
}
async function testAggregateShrinking(count: number, method: 'pop' | 'shift') {
const mol_wire_lib = await import('mol_wire_lib')
const { $mol_wire_atom } = mol_wire_lib.default
const { atom, createCtx } = await import('@reatom/core')
const V4 = await import('../../reatom4/packages/core/build')
const { observable, computed, autorun, configure } = await import('mobx')
configure({ enforceActions: 'never' })
const molAtoms = Array.from(
{ length: count },
(_, i) => new $mol_wire_atom(`${i}`, (next: number = 1) => next),
)
const reAtoms = Array.from({ length: count }, (_, i) => atom(1, `${i}`))
const V4Atoms = Array.from({ length: count }, (_, i) => V4.atom(1, `${i}`))
const mobxAtoms = Array.from({ length: count }, (_, i) =>
observable.box(1, { name: `${i}` }),
)
const molAtom = new $mol_wire_atom(`sum`, () =>
molAtoms.reduce((sum, atom) => sum + atom.sync(), 0),
)
const reAtom = atom(
(ctx) => reAtoms.reduce((sum, atom) => sum + ctx.spy(atom), 0),
`sum`,
)
const V4Atom = V4.atom(
() => V4Atoms.reduce((sum, atom) => sum + atom(), 0),
`sum`,
)
const mobxAtom = computed(
() => mobxAtoms.reduce((sum, atom) => sum + atom.get(), 0),
{ name: `sum` },
)
const ctx = createCtx()
const V4Root = V4.AsyncContext.Snapshot.createRoot()
ctx.subscribe(reAtom, () => {})
V4.effect(() => V4Atom()).run(V4Root.frame)
molAtom.sync()
autorun(() => mobxAtom.get())
const reatomLogs = new Array<number>()
const V4Logs = new Array<number>()
const molLogs = new Array<number>()
const mobxLogs = new Array<number>()
let i = 1
while (i++ < count) {
const startReatom = performance.now()
reAtoms[method]()!(ctx, i)
reatomLogs.push(performance.now() - startReatom)
const startV4 = performance.now()
V4Root.run(() => {
V4Atoms[method]()!(i)
V4.notify()
})
V4Logs.push(performance.now() - startV4)
const startMol = performance.now()
molAtoms[method]()!.put(i)
molAtom.sync()
molLogs.push(performance.now() - startMol)
const startMobx = performance.now()
mobxAtoms[method]()!.set(i)
mobxLogs.push(performance.now() - startMobx)
await new Promise((resolve) => setTimeout(resolve, 0))
}
if (
new Set([
molAtom.sync(),
ctx.get(reAtom),
V4Root.run(V4Atom),
mobxAtom.get(),
]).size > 1
) {
throw new Error(
'Mismatch: ' +
JSON.stringify({
mol: molAtom.sync(),
reatom: ctx.get(reAtom),
V4: V4Root.run(V4Atom),
mobx: mobxAtom.get(),
}),
)
}
console.log(
`Median of sum calc of reactive nodes in list from ${count} to 1 (with "${method}")`,
)
return printLogs({
reatom: formatLog(reatomLogs),
$mol_wire: formatLog(molLogs),
mobx: formatLog(mobxLogs),
// act: formatLog(actLogs),
V4: formatLog(V4Logs),
})
}
async function testParent(count: number) {
const mol_wire_lib = await import('mol_wire_lib')
const { $mol_wire_atom } = mol_wire_lib.default
const { atom, createCtx } = await import('@reatom/core')
const V4 = await import('../../reatom4/packages/core/build')
const { observable, computed, autorun, configure } = await import('mobx')
configure({ enforceActions: 'never' })
const molAtom = new $mol_wire_atom(`0`, (next: number = 0) => next)
const molAtoms = []
const reAtom = atom(0, `${0}`)
const V4Atom = V4.atom(0, `${0}`)
const mobxAtom = observable.box(0, { name: `${0}` })
const ctx = createCtx()
const V4Root = V4.AsyncContext.Snapshot.createRoot()
{
let i = count
while (i--) {
const molPubAtom = new $mol_wire_atom(`${i}`, () => molAtom.sync())
molPubAtom.sync()
molAtoms.push(molPubAtom)
ctx.subscribe(
atom((ctx) => ctx.spy(reAtom)),
() => {},
)
const V4DepAtom = V4.atom(() => V4Atom())
V4.effect(() => V4DepAtom()).run(V4Root.frame)
const mobxDepAtom = computed(() => mobxAtom.get())
autorun(() => mobxDepAtom.get())
}
}
const reatomLogs = new Array<number>()
const V4Logs = new Array<number>()
const molLogs = new Array<number>()
const mobxLogs = new Array<number>()
let i = count
while (i--) {
const startReatom = performance.now()
reAtom(ctx, i)
reatomLogs.push(performance.now() - startReatom)
const startV4 = performance.now()
V4Root.run(() => {
V4Atom(i)
V4.notify()
})
V4Logs.push(performance.now() - startV4)
const startMol = performance.now()
molAtom.put(i)
molAtoms.forEach((atom) => atom.sync())
molLogs.push(performance.now() - startMol)
const startMobx = performance.now()
mobxAtom.set(i)
mobxLogs.push(performance.now() - startMobx)
await new Promise((resolve) => setTimeout(resolve, 0))
}
if (
new Set([
molAtom.sync(),
ctx.get(reAtom),
V4Root.run(V4Atom),
mobxAtom.get(),
]).size > 1
) {
throw new Error(
'Mismatch: ' +
JSON.stringify({
mol: molAtom.sync(),
reatom: ctx.get(reAtom),
V4: V4Root.run(V4Atom),
mobx: mobxAtom.get(),
}),
)
}
console.log(`Median of update 1 node with ${count} reactive children`)
return printLogs({
reatom: formatLog(reatomLogs),
$mol_wire: formatLog(molLogs),
mobx: formatLog(mobxLogs),
// act: formatLog(actLogs),
V4: formatLog(V4Logs),
})
}
;(async () => {
// const subscribers = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
const subscribers = [1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 10]
for (const i of subscribers) {
var results = [
await testAggregateGrowing(i, 'push'),
await testAggregateGrowing(i, 'unshift'),
await testAggregateShrinking(i, 'pop'),
await testAggregateShrinking(i, 'shift'),
// await testParent(i),
]
}
console.log('\nAVERAGE for', subscribers.join(','), 'subscribers')
Object.keys(results![0])
.map((name) => ({
name,
pos:
results!.reduce((acc, log) => +log[name][POSITION_KEY] + acc, 0) /
results!.length,
}))
.sort((a, b) => a.pos - b.pos)
.forEach(({ name, pos }) => console.log(pos, name))
process.exit()
})()