/
bench.js
91 lines (81 loc) · 2.18 KB
/
bench.js
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
const Benchmark = require('benchmark');
const DeepState = require('./index.cjs.js');
const width = 10;
const height = 10000;
const subs = 1000;
function getObj() {
const obj = {};
for (let h = 0; h < height; h++) {
const current = {};
for (let w = 0; w < width; w++) {
current['w' + w] = `${h} ${w}`;
}
obj['h' + h] = current;
}
return obj;
}
const item = `h${Math.round(height / 2)}.w${Math.round(width / 2)}`;
const wild = `h${Math.round(height / 2)}.*${Math.round(width / 2)}`;
function generateSubs(state) {
for (let i = 0; i < subs; i++) {
state.subscribe(wild, () => {
const x = 1 + Math.random();
});
state.subscribe(item, () => {
const x = 1 + Math.random();
});
}
}
const state = new Array(2);
const objs = [getObj(), getObj()];
state[0] = new DeepState(objs[0], { useCache: false });
generateSubs(state[0]);
state[1] = new DeepState(objs[1], { useCache: true });
generateSubs(state[1]);
console.log('update & get');
new Benchmark.Suite('update get')
.add('[no cache]', function () {
state[0].update(item, { val: Math.random() });
state[0].get(wild);
})
.add('[cache]', function () {
state[1].update(item, 2);
state[1].get(wild);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log(`Fastest is '${this.filter('fastest').map('name')}'`);
})
.run();
console.log('update');
new Benchmark.Suite('update')
.add('[no cache]', function () {
state[0].update(item, { val: Math.random() });
})
.add('[cache]', function () {
state[1].update(item, { val: Math.random() });
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log(`Fastest is '${this.filter('fastest').map('name')}'`);
})
.run();
console.log('get');
new Benchmark.Suite('get')
.add('[no cache]', function () {
state[0].get(wild);
})
.add('[cache]', function () {
state[1].get(wild);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log(`Fastest is '${this.filter('fastest').map('name')}'`);
})
.run();