Skip to content

Instantly share code, notes, and snippets.

@binyamin
Created October 8, 2020 21:08
Show Gist options
  • Save binyamin/e1fafe819b1478c18c2db184449c9d0a to your computer and use it in GitHub Desktop.
Save binyamin/e1fafe819b1478c18c2db184449c9d0a to your computer and use it in GitHub Desktop.
Timing functions in node.js
const NS_PER_SEC = 1e9;
function test(func, ...args) {
let times = [];
for (let i = 0; i < 5; i++) {
const t = process.hrtime();
func(...args);
const diff = process.hrtime(t);
times.push((diff[0] * NS_PER_SEC) + diff[1]);
}
return times;
}
function avg(o) {
return o.reduce((a,b,i) => a+b) / o.length;
}
let a = test(func_a, "args");
let b = test(func_b, "args");
console.log(a > b ? "a won" : "b won");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment