Sometimes, we want to get a microtime in Node.js.
In this article, we’ll look at how to get a microtime in Node.js.
How to get a microtime in Node.js?
To get a microtime in Node.js, we can use the process.hrtime method.
For instance, we write
const [seconds, nanoSeconds] = process.hrtime();
console.log(seconds * 1000000 + nanoSeconds / 1000);
to call process.hrtime to return an array with the current timestamp as an array with the seconds and nanoSeconds.
Then we use seconds * 1000000 + nanoSeconds / 1000 to convert seconds and nanoSeconds to microseconds and add them together.
Conclusion
To get a microtime in Node.js, we can use the process.hrtime method.