This commit is contained in:
Brooke Vibber 2023-09-04 07:42:24 -07:00
commit 0592fb71f9

73
speci.js Normal file
View file

@ -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();