Categories
JavaScript Answers

How to get a microtime in Node.js?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *