MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add regression tests for arrays

+134
+38
tests/bench_includes_dense.cjs
··· 1 + function 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 + 28 + const arr = []; 29 + for (let i = 0; i < 1024; i++) arr.push(i); 30 + 31 + runBench("dense includes", () => { 32 + let hits = 0; 33 + for (let r = 0; r < 20000; r++) { 34 + if (arr.includes(1023)) hits++; 35 + if (arr.includes(-1)) hits++; 36 + } 37 + return hits; 38 + });
+60
tests/bench_includes_generic_proxy.cjs
··· 1 + function 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 + 28 + const generic = { 29 + get length() { 30 + return 1024; 31 + }, 32 + get 1023() { 33 + return 1; 34 + } 35 + }; 36 + 37 + const 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 + 46 + runBench("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 + 54 + runBench("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 + });
+36
tests/bench_includes_sparse.cjs
··· 1 + function 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 + 28 + const arr = Array(1024); 29 + 30 + runBench("sparse includes(undefined)", () => { 31 + let hits = 0; 32 + for (let r = 0; r < 50000; r++) { 33 + if (arr.includes(undefined)) hits++; 34 + } 35 + return hits; 36 + });