QA
Intermediate LevelNode JS

How does process.hrtime.bigint() provide nanosecond-precision timing for performance benchmarking?

❓ Interview Prompt: const start = process.hrtime.bigint(); executeCode(); const end = process.hrtime.bigint(); const nanoseconds = end - start;. Unaffected by OS clock drift or NTP synchronization adjustments.

Standard 'Date.now()' is subject to OS clock drift and NTP synchronization adjustments. 'process.hrtime.bigint()' returns a monotonic high-resolution real-time timestamp in nanoseconds (as a 'BigInt'):

Benefit:

Monotonic clock guarantee: Never goes backward, making it ideal for micro-benchmarking latency!

Code Implementation Example

javascript
const start = process.hrtime.bigint();

// Perform heavy calculation
let sum = 0;
for (let i = 0; i < 1e6; i++) sum += i;

const end = process.hrtime.bigint();
const durationNs = end - start;
const durationMs = Number(durationNs) / 1e6;

console.log('Execution Time: ${durationMs.toFixed(3)}ms (${durationNs}ns)');
💡Measuring nanosecond-precision code execution latency with process.hrtime.bigint.
🎯 Interview Key Takeaway

Use process.hrtime.bigint() for monotonic, nanosecond-precision code benchmarking.

#hrtime#BigInt#Benchmarking#Latency#Intermediate
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?