-
Notifications
You must be signed in to change notification settings - Fork 23
/
holey_array.js
111 lines (85 loc) · 2.27 KB
/
holey_array.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { run, bench, summary, do_not_optimize } from 'mitata';
// https://v8.dev/blog/elements-kinds
summary(() => {
bench('sum(holey $size)', function* (state) {
const size = state.get('size');
const arr = new Array(size);
for (let o = 0; o < size; o++) arr[o] = 1;
yield {
[0]() {
return arr;
},
bench(arr) {
let i = 0;
const len = arr.length;
for (let o = 0; o < len; o++) i += arr[o];
return do_not_optimize(i);
},
};
}).range('size', 1, 1024);
bench('sum(packed $size)', function* (state) {
const size = state.get('size');
const arr = Array.from({ length: size }, () => 1);
yield {
[0]() {
return arr;
},
bench(arr) {
let i = 0;
const len = arr.length;
for (let o = 0; o < len; o++) i += arr[o];
return do_not_optimize(i);
},
};
}).range('size', 1, 1024);
});
summary(() => {
bench('new Array + sum(holey $size)', function* (state) {
const size = state.get('size');
yield {
[0]() {
return size;
},
bench(size) {
const arr = new Array(size);
for (let o = 0; o < size; o++) arr[o] = 1;
let i = 0;
const len = arr.length;
for (let o = 0; o < len; o++) i += arr[o];
return do_not_optimize(i);
},
};
}).range('size', 1, 1024);
bench('Array.from + sum(packed $size)', function* (state) {
const size = state.get('size');
yield {
[0]() {
return size;
},
bench(size) {
const arr = Array.from({ length: size }, () => 1);
let i = 0;
const len = arr.length;
for (let o = 0; o < len; o++) i += arr[o];
return do_not_optimize(i);
},
};
}).range('size', 1, 1024);
bench('arr.push() + sum(packed $size)', function* (state) {
const size = state.get('size');
yield {
[0]() {
return size;
},
bench(size) {
const arr = [];
for (let o = 0; o < size; o++) arr.push(1);
let i = 0;
const len = arr.length;
for (let o = 0; o < len; o++) i += arr[o];
return do_not_optimize(i);
},
};
}).range('size', 1, 1024);
});
await run();