Last active
November 27, 2020 06:51
-
-
Save vipinrana/ccf693f6c133b8cca13276504bacbd38 to your computer and use it in GitHub Desktop.
measuring different execution time in node js
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
const {performance} = require('perf_hooks'); | |
// using hrtime API | |
const hrBefore = process.hrtime(); | |
setTimeout(function () { | |
const hrAfter = process.hrtime(hrBefore); | |
console.log(`Using hrtime ${hrAfter[0] * 1e3 + hrAfter[1] / 1e6}ms`); | |
}, 0); | |
// using the hrtimebigint API | |
const hrBigBefore = process.hrtime.bigint(); | |
setTimeout(function () { | |
const diff = process.hrtime.bigint() - hrBigBefore; | |
console.log(`Using hrtime bigint ${Number(diff) / 1e6}ms`); | |
}, 0); | |
//using the performance API | |
const perBefore = performance.now(); | |
setTimeout(function () { | |
const diff = performance.now() - perBefore; | |
console.log(`Using perf hooks ${diff}ms`); | |
}, 0); | |
// using the console time API | |
console.time('Using timer '); | |
setTimeout(function () { | |
console.timeEnd('Using timer '); | |
}, 0); |
How about
process.hrtime.bigint()
?
Yes! We can also use that if you care about the execution time in BigInt.
Also, there are some difference between process.hrtime() and process.hrtime.bigint() :-
-
process.hrtime() is the legacy version of process.hrtime.bigint().
-
process.hrtime.bigint() doesn't accept any argument
-
process.hrtime() give time in [seconds, nanoseconds] while process.hrtime.bigint() in nanoseconds.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about
process.hrtime.bigint()
?