Skip to content

Instantly share code, notes, and snippets.

@cfuj
Created June 5, 2021 23:14
Show Gist options
  • Save cfuj/5dc590ee013a730dfb00751096d8c616 to your computer and use it in GitHub Desktop.
Save cfuj/5dc590ee013a730dfb00751096d8c616 to your computer and use it in GitHub Desktop.
Generic JS timer script for testing performance
let averageOf = (array) => array.reduce((a, b) => a + b) / array.length;
let minOf = (array) => Math.min.apply(Math, array)
let maxOf = (array) => Math.max.apply(Math, array)
let request = {};
request.i = 1;
request.iterations = 1000;
request.tests = 10;
function timer(f, args) {
args = args || {};
var durations = [];
for (x = 1; x <= request.tests; x++) {
var startTimer = new Date().getMilliseconds();
for (request.i = 1; request.i <= request.iterations; request.i++) {
f.apply(this, args);
}
durations.push(new Date().getMilliseconds() - startTimer);
}
console.log(f.name + "\n" +
"range(ms): " + minOf(durations) + "-" + maxOf(durations) + "\n" +
"average: " + averageOf(durations));
}
function a() {
}
function b() {
}
timer(a);
timer(b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment