function doit(mode) { function make_x_first(x,y) { return {x, y}; } function make_y_first(x, y) { return {y, x}; } function make_mixed(x, y) { if (Math.random() > 0.5) { return make_y_first(x, y); } else { return make_x_first(x, y); } } function make_rand_prefix(x, y) { let rando = (Math.random() * (2 ** 32 - 1)) | 0; let r = 'random' + rando; let o = {[r]: r, x, y}; return o; } function make_rand_suffix(x, y) { let rando = (Math.random() * (2 ** 32 - 1)) | 0; let r = 'random' + rando; let o = {x, y, [r]: r}; return o; } const million = 1000 * 1000; const n = 1 * million; const runs = 100 * n; function bulk(filler) { let a = []; for (let i = 0; i < n; i++) { a.push(filler(i, i * i)); } return a; } let funcMap = { 'x-first': make_x_first, 'y-first': make_y_first, 'mixed': make_mixed, 'rand-prefix': make_rand_prefix, 'rand-suffix': make_rand_suffix, }; let func = funcMap[mode]; if (!func) { console.log(`Pass as parameter one of:`); for (let key of Object.keys(funcMap)) { console.log(` ${key}`); } process.exit(1); } const data = bulk(func); 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 < runs; i++) { let item = dataSet[i % dataSet.length]; sumX += item.x; sumY += item.y; } return {x: sumX, y: sumY}; }); console.log(``); } } console.log(`** ${mode}`); demo(data); } let mode = String(process.argv[2]); doit(mode);