Stress test on sumVectorObjectLikeArray: 122732 ms
NodeJS is not magic, and you are not a Wizard
Ivan Seidel Gomes
593

Again, this benchmark test has the same problem as the previous one: there is too much noise.

I ran the benchmark test with input data being preallocated, these are the results:

Stress test on sumVectorObjectLikeArray2: 393 ms
Stress test on sumVectorObject2: 640 ms
Stress test on sumVectorArray2: 262 ms

The source code:

const child_process = require(‘child_process’);
const sleep = (sec = 1) => {
 child_process.execSync(`sleep ${sec}s`);
}
const sumVectorObject2 = (a, b) => {
 let c = {x: a.x + b.x, y: a.y + b.x}
 return c;
}
// As Array
const sumVectorArray2 = (a, b) => {
 let c = [a[0] + b[0], a[1] + b[1]]
 return c;
}
function makeItBurn2(name, benchMethod, input) {
 let timeStart = Date.now()
let N = input.length/2;
 for (let i = 0; i < N; i++) {
 benchMethod(input[i], input[i+1]);
 }
 let timeEnd = Date.now()
 let duration = (timeEnd — timeStart)
 console.log(`Stress test on ${name}: ${duration} ms`)
}
const N = 6 * 1000 * 1000;
var input;
input = [];
for (let i = 0; i < N; i++) {
 input.push({0: Math.random(), 1: Math.random()});
 input.push({0: Math.random(), 1: Math.random()});
}
makeItBurn2(‘sumVectorObjectLikeArray2’, sumVectorArray2, input);
input = [];
// Give GC a chance
sleep(1);
input = [];
for (let i = 0; i < N; i++) {
 input.push([Math.random(), Math.random()]);
 input.push([Math.random(), Math.random()]);
}
makeItBurn2(‘sumVectorObject2’, sumVectorObject2, input);
input = [];
// Give GC a chance
sleep(2);
input = [];
for (let i = 0; i < N; i++) {
 input.push({x: Math.random(), y: Math.random()});
 input.push({x: Math.random(), y: Math.random()});
}
makeItBurn2(‘sumVectorArray2’, sumVectorArray2, input);