Created
October 8, 2020 21:08
-
-
Save binyamin/e1fafe819b1478c18c2db184449c9d0a to your computer and use it in GitHub Desktop.
Timing functions in node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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