-
Notifications
You must be signed in to change notification settings - Fork 5
/
reactiveArray.js
95 lines (82 loc) · 2.42 KB
/
reactiveArray.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
92
93
94
95
function go() {
const { ref, computed, reactive, shallowRef, triggerRef, readonly, toRaw } = Vue;
const suite = new Benchmark.Suite();
for (let amount = 10; amount < 1e6; amount *= 10) {
bench(() => {
const rawArray = [];
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
const r = reactive(rawArray);
const c = computed(() => {
return r.reduce((v, a) => a + v, 0)
});
return suite.add(`reduce *reactive* array, ${amount} elements`, () => {
for (let i = 0, n = r.length; i < n; i++) {
r[i]++
}
const value = c.value
});
});
bench(() => {
const rawArray = [];
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
const r = reactive(rawArray);
const c = computed(() => {
return r.reduce((v, a) => a + v, 0)
});
return suite.add(`reduce *reactive* array, ${amount} elements, only change first value`, () => {
r[0]++
const value = c.value
});
});
bench(() => {
const rawArray = [];
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
const r = reactive({ arr: readonly(rawArray) });
const c = computed(() => {
return r.arr.reduce((v, a) => a + v, 0)
});
return suite.add(`reduce *readonly* array, ${amount} elements`, () => {
r.arr = r.arr.map(v => v + 1)
const value = c.value
});
});
bench(() => {
const rawArray = [];
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
const r = shallowRef(rawArray);
const c = computed(() => {
return r.value.reduce((v, a) => a + v, 0)
});
return suite.add(`reduce *raw* array, copied, ${amount} elements`, () => {
r.value = r.value.map(v => v + 1)
const value = c.value
});
});
bench(() => {
const rawArray = [];
for (let i = 0, n = amount; i < n; i++) {
rawArray.push(i)
}
const r = shallowRef(rawArray);
const c = computed(() => {
return r.value.reduce((v, a) => a + v, 0)
});
return suite.add(`reduce *raw* array, manually triggered, ${amount} elements`, () => {
for (let i = 0, n = rawArray.length; i < n; i++) {
rawArray[i]++
}
triggerRef(r);
const value = c.value
});
});
}
return suite;
}