If you were to test the execution time of your Node.js code, which timing function would you choose? The first options that typically come to mind are Date.now
or Date.getTime
.
Conclusion First:
In Node.js programs, prioritize process.hrtime
, followed by performance.now
, and lastly Date.now
.
This preference is based on considerations of precision and clock synchronization.
Date.now
To achieve higher precision and independence from system time, the W3C established the High Resolution Time Level 2 standard. The 6. Monotonic Clock section specifies that standard-compliant implementations must provide a "monotonic" global system clock.
This standard has been implemented in both Node.js and browsers via the performance
object. We can use performance.now
to obtain a timestamp relative to a starting point, with the following features:
Date.now
), performance.now()
returns a floating-point number, achieving microsecond (10^-6) precision.performance.now()
allows for clock drift.A Brief Note on Clock Drift
Clock drift arises from the concept of clock synchronization, which aims to align multiple independent clocks. In reality, even after synchronization, clocks may drift over time due to slight differences in their ticking rates, leading to varying displayed times.
Is There a More Precise Clock?
Yes, in Node.js, there is the process.hrtime
method:
The process.hrtime
method is specifically designed for measuring time intervals.
Note: The browser environment does not support hrtime
, so the best precision achievable in browsers is the microsecond level via performance.now
(subject to implementation differences across browsers).
However, using process.hrtime
requires attention to its usage. The first call returns a time
value, which must be used as an input parameter for subsequent calls:
const NS_PER_SEC = 1e9;
const time = process.hrtime(); // First call, returns the initial `time` variable
// [ 1800216, 25 ]
setTimeout(() => {
const diff = process.hrtime(time); // Use the first returned `time` as input for the second call to calculate the time difference
// [ 1, 552 ]
console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`);
// Benchmark took 1000000552 nanoseconds
}, 1000);
This concludes the main content and naturally leads to the conclusion at the beginning of this section.
You can also use the hrtime.bigint
method. This is a BigInt-based version of process.hrtime
(BigInt support was introduced in v10.4), which provides the current high-precision actual time.
This method is more convenient than process.hrtime
because it does not require an additional time
input parameter. You can calculate the time difference by simply subtracting the results of two calls:
const start = process.hrtime.bigint();
// 191051479007711n
setTimeout(() => {
const end = process.hrtime.bigint();
// 191052633396993n
console.log(`Benchmark took ${end - start} nanoseconds`);
// Benchmark took 1154389282 nanoseconds
}, 1000);
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
Deploy unlimited projects for free
Unbeatable Cost Efficiency
Streamlined Developer Experience
Effortless Scalability and High Performance
Explore more in the Documentation!
Follow us on X: @LeapcellHQ