Last active
November 24, 2020 15:17
-
-
Save townofdon/a375605891299f9233e4f7fc553464f7 to your computer and use it in GitHub Desktop.
dead-simple benchmarking tool
This file contains 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
'use strict'; | |
/** | |
* Benchmark the sh#t out of stuff | |
* Use this in NodeJS environments. | |
* Shamelessly borrowed from: https://www.stefanjudis.com/today-i-learned/measuring-execution-time-more-precisely-in-the-browser-and-node-js/ | |
* | |
* USAGE: | |
* `const benchmark = Benchmark(); // starts the 'clock'` | |
* `benchmark(); // logs current elapsed ns since start or last benchmark` | |
*/ | |
module.exports = function Benchmark() { | |
const NS_PER_SEC = 1e9; | |
let time = process.hrtime(); | |
return () => { | |
const diff = process.hrtime(time); | |
console.log(`${(diff[0] * NS_PER_SEC + diff[1]) / NS_PER_SEC}s`); | |
time = process.hrtime(); | |
}; | |
} |
This file contains 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
'use strict'; | |
/** | |
* Benchmark the sh#t out of stuff | |
* Use this in web browsers. | |
* | |
* USAGE: | |
* `const benchmark = new Benchmark(); // starts the 'clock'` | |
* `benchmark(); // logs current elapsed ms since start` | |
*/ | |
module.exports = function Benchmark() { | |
let time = new Date().getTime(); | |
return () => { | |
console.log(new Date().getTime() - time); | |
time = new Date().getTime(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment