-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbench.js
More file actions
49 lines (41 loc) · 1.41 KB
/
bench.js
File metadata and controls
49 lines (41 loc) · 1.41 KB
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
import FlatQueue from './index.js';
const N = 1000000;
const K = 1000;
const WARMUP = 3;
const RUNS = 10;
const data = [];
for (let i = 0; i < N; i++) data[i] = Math.round(1000 * Math.random());
// Set to `() => new FlatQueue()` to compare against regular arrays.
const create = () => new FlatQueue(N, Uint16Array);
// Runs `fn` WARMUP + RUNS times, printing the best and median wall-clock time.
// `setup` (if given) runs before each iteration and is not timed.
function bench(name, fn, setup) {
const times = [];
for (let run = 0; run < WARMUP + RUNS; run++) {
const arg = setup && setup();
const start = performance.now();
fn(arg);
const elapsed = performance.now() - start;
if (run >= WARMUP) times.push(elapsed);
}
times.sort((a, b) => a - b);
const best = times[0];
const median = times[times.length >> 1];
console.log(`${name}: ${best.toFixed(2)}ms best, ${median.toFixed(2)}ms median`);
}
bench(`push ${N}`, (f) => {
for (let i = 0; i < N; i++) f.push(i, data[i]);
}, create);
bench(`pop ${N}`, (f) => {
for (let i = 0; i < N; i++) f.pop();
}, () => {
const f = create();
for (let i = 0; i < N; i++) f.push(i, data[i]);
return f;
});
bench(`push/pop ${N} (K=${K})`, (f) => {
for (let i = 0; i < N; i += K) {
for (let j = 0; j < K; j++) f.push(i, data[i + j]);
for (let j = 0; j < K; j++) f.pop();
}
}, create);