From 0592fb71f975c189865bab20ac8b43a45baac8ee Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 4 Sep 2023 07:42:24 -0700 Subject: [PATCH] initial --- speci.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 speci.js diff --git a/speci.js b/speci.js new file mode 100644 index 0000000..9b39db5 --- /dev/null +++ b/speci.js @@ -0,0 +1,73 @@ +function doit() { + + function make_x_first(x,y) { + return {x, y}; + } + + function make_y_first(x, y) { + return {y, x}; + } + + function make_random(x, y) { + if (Math.random() > 0.5) { + return make_y_first(x, y); + } else { + return make_x_first(x, y); + } + } + + const million = 1000 * 1000; + const n = 10 * million; + + function bulk(filler) { + let a = []; + for (let i = 0; i < n; i++) { + a.push(filler(i, i * i)); + } + return a; + } + + const x_first = bulk(make_x_first); + const y_first = bulk(make_y_first); + const mixed = bulk(make_random); + + function time(func) { + const start = Date.now(); + func(); + const delta = Date.now() - start; + return delta; + } + + function report(func) { + let delta = time(func); + console.log(`${delta} ms`); + } + + function demo(dataSet) { + for (let i = 0; i < 3; i++) { + console.log(`run ${i+1}`); + report(() => { + let sumX = 0, sumY = 0; + for (let i = 0; i < dataSet.length; i++) { + let item = dataSet[i]; + sumX += item.x; + sumY += item.y; + } + return {x: sumX, y: sumY}; + }); + console.log(``); + } + } + + console.log(`** x_first`); + demo(x_first); + + console.log(`** y_first`); + demo(y_first); + + console.log(`** mixed`); + demo(mixed); + +} + +doit(); \ No newline at end of file