MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1function runBench(label, fn, options) {
2 const warmup = options && options.warmup ? options.warmup : 5;
3 const samples = options && options.samples ? options.samples : 10;
4
5 for (let i = 0; i < warmup; i++) fn();
6
7 let min = Infinity;
8 let max = -Infinity;
9 let total = 0;
10 let last = 0;
11
12 for (let i = 0; i < samples; i++) {
13 const start = performance.now();
14 last = fn();
15 const elapsed = performance.now() - start;
16 if (elapsed < min) min = elapsed;
17 if (elapsed > max) max = elapsed;
18 total += elapsed;
19 }
20
21 console.log(label);
22 console.log(` result: ${last}`);
23 console.log(` mean: ${ (total / samples).toFixed(3) } ms`);
24 console.log(` min: ${ min.toFixed(3) } ms`);
25 console.log(` max: ${ max.toFixed(3) } ms`);
26}
27
28const generic = {
29 get length() {
30 return 1024;
31 },
32 get 1023() {
33 return 1;
34 }
35};
36
37const proxy = new Proxy(
38 { length: 4, 0: NaN, 1: 0, 2: NaN, 3: 1 },
39 {
40 get(target, key) {
41 return target[key];
42 }
43 }
44);
45
46runBench("generic array-like with getter", () => {
47 let hits = 0;
48 for (let r = 0; r < 20000; r++) {
49 if ([].includes.call(generic, 1, 1023)) hits++;
50 }
51 return hits;
52});
53
54runBench("proxy includes", () => {
55 let hits = 0;
56 for (let r = 0; r < 20000; r++) {
57 if (Array.prototype.includes.call(proxy, NaN, 1)) hits++;
58 }
59 return hits;
60});