diff --git a/tests/bench_includes_dense.cjs b/tests/bench_includes_dense.cjs new file mode 100644 index 0000000..665447a --- /dev/null +++ b/tests/bench_includes_dense.cjs @@ -0,0 +1,38 @@ +function runBench(label, fn, options) { + const warmup = options && options.warmup ? options.warmup : 5; + const samples = options && options.samples ? options.samples : 10; + + for (let i = 0; i < warmup; i++) fn(); + + let min = Infinity; + let max = -Infinity; + let total = 0; + let last = 0; + + for (let i = 0; i < samples; i++) { + const start = performance.now(); + last = fn(); + const elapsed = performance.now() - start; + if (elapsed < min) min = elapsed; + if (elapsed > max) max = elapsed; + total += elapsed; + } + + console.log(label); + console.log(` result: ${last}`); + console.log(` mean: ${ (total / samples).toFixed(3) } ms`); + console.log(` min: ${ min.toFixed(3) } ms`); + console.log(` max: ${ max.toFixed(3) } ms`); +} + +const arr = []; +for (let i = 0; i < 1024; i++) arr.push(i); + +runBench("dense includes", () => { + let hits = 0; + for (let r = 0; r < 20000; r++) { + if (arr.includes(1023)) hits++; + if (arr.includes(-1)) hits++; + } + return hits; +}); diff --git a/tests/bench_includes_generic_proxy.cjs b/tests/bench_includes_generic_proxy.cjs new file mode 100644 index 0000000..4fae5dd --- /dev/null +++ b/tests/bench_includes_generic_proxy.cjs @@ -0,0 +1,60 @@ +function runBench(label, fn, options) { + const warmup = options && options.warmup ? options.warmup : 5; + const samples = options && options.samples ? options.samples : 10; + + for (let i = 0; i < warmup; i++) fn(); + + let min = Infinity; + let max = -Infinity; + let total = 0; + let last = 0; + + for (let i = 0; i < samples; i++) { + const start = performance.now(); + last = fn(); + const elapsed = performance.now() - start; + if (elapsed < min) min = elapsed; + if (elapsed > max) max = elapsed; + total += elapsed; + } + + console.log(label); + console.log(` result: ${last}`); + console.log(` mean: ${ (total / samples).toFixed(3) } ms`); + console.log(` min: ${ min.toFixed(3) } ms`); + console.log(` max: ${ max.toFixed(3) } ms`); +} + +const generic = { + get length() { + return 1024; + }, + get 1023() { + return 1; + } +}; + +const proxy = new Proxy( + { length: 4, 0: NaN, 1: 0, 2: NaN, 3: 1 }, + { + get(target, key) { + return target[key]; + } + } +); + +runBench("generic array-like with getter", () => { + let hits = 0; + for (let r = 0; r < 20000; r++) { + if ([].includes.call(generic, 1, 1023)) hits++; + } + return hits; +}); + +runBench("proxy includes", () => { + let hits = 0; + for (let r = 0; r < 20000; r++) { + if (Array.prototype.includes.call(proxy, NaN, 1)) hits++; + } + return hits; +}); diff --git a/tests/bench_includes_sparse.cjs b/tests/bench_includes_sparse.cjs new file mode 100644 index 0000000..39c4c9d --- /dev/null +++ b/tests/bench_includes_sparse.cjs @@ -0,0 +1,36 @@ +function runBench(label, fn, options) { + const warmup = options && options.warmup ? options.warmup : 5; + const samples = options && options.samples ? options.samples : 10; + + for (let i = 0; i < warmup; i++) fn(); + + let min = Infinity; + let max = -Infinity; + let total = 0; + let last = 0; + + for (let i = 0; i < samples; i++) { + const start = performance.now(); + last = fn(); + const elapsed = performance.now() - start; + if (elapsed < min) min = elapsed; + if (elapsed > max) max = elapsed; + total += elapsed; + } + + console.log(label); + console.log(` result: ${last}`); + console.log(` mean: ${ (total / samples).toFixed(3) } ms`); + console.log(` min: ${ min.toFixed(3) } ms`); + console.log(` max: ${ max.toFixed(3) } ms`); +} + +const arr = Array(1024); + +runBench("sparse includes(undefined)", () => { + let hits = 0; + for (let r = 0; r < 50000; r++) { + if (arr.includes(undefined)) hits++; + } + return hits; +});