Skip to content

Instantly share code, notes, and snippets.

@vipinrana
Last active November 27, 2020 06:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vipinrana/ccf693f6c133b8cca13276504bacbd38 to your computer and use it in GitHub Desktop.
Save vipinrana/ccf693f6c133b8cca13276504bacbd38 to your computer and use it in GitHub Desktop.
measuring different execution time in node js
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);
@RinatValiullov
Copy link

@vipinrana
Copy link
Author

vipinrana commented Nov 16, 2020

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