Nice, I just wrote something similar, with the standard deviation now that I am getting individual run times from performance.now()
.
It’s good to know that my gut is right with those rules of thumb. I’ve been bitten bad by trying to perform memory expensive hijinks to only pass through the data once, so I’ve learned the hard way.
With my first stab at reduction, you can tell that I’m not as comfortable with built in methods in JavaScript yet
// Compute stats
let totalTime = times[0];
let minTime = times[0];
let maxTime = times[0];
for (let j = 1; j < numRuns; j++) {
totalTime += times[j];
minTime = times[j] < minTime ? times[j] : minTime;
maxTime = times[j] > maxTime ? times[j] : maxTime;
}
const avgTime = totalTime/numRuns;
let variance = 0;
for (let j = 0; j < numRuns; j++)
variance += (avgTime - times[j])**2;
variance /= numRuns;
const stdDev = Math.sqrt(variance);
I’ll probably clean it up a bit with inspiration from your script : )