-
-
Notifications
You must be signed in to change notification settings - Fork 568
/
subscribe-write.ts
executable file
路59 lines (54 loc) 路 1.31 KB
/
subscribe-write.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
#!/usr/bin/env npx tsx
import { add, complete, cycle, save, suite } from 'benny'
import { atom } from '../src/vanilla/atom.ts'
import type { PrimitiveAtom } from '../src/vanilla/atom.ts'
import { createStore } from '../src/vanilla/store.ts'
const cleanupFns = new Set<() => void>()
const cleanup = () => {
cleanupFns.forEach((fn) => fn())
cleanupFns.clear()
}
const createStateWithAtoms = (n: number) => {
let targetAtom: PrimitiveAtom<number> | undefined
const store = createStore()
for (let i = 0; i < n; ++i) {
const a = atom(i)
if (!targetAtom) {
targetAtom = a
}
store.get(a)
const unsub = store.sub(a, () => {
store.get(a)
})
cleanupFns.add(unsub)
}
if (!targetAtom) {
throw new Error()
}
return [store, targetAtom] as const
}
const main = async () => {
await suite(
'subscribe-write',
...[2, 3, 4, 5, 6].map((n) =>
add(`atoms=${10 ** n}`, () => {
cleanup()
const [store, targetAtom] = createStateWithAtoms(10 ** n)
return () => store.set(targetAtom, (c) => c + 1)
}),
),
cycle(),
complete(),
save({
folder: __dirname,
file: 'subscribe-write',
format: 'json',
}),
save({
folder: __dirname,
file: 'subscribe-write',
format: 'chart.html',
}),
)
}
main()