Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.hrtime() and hrtime.bigint()

Reference for process.hrtime() and process.hrtime.bigint() in Node.js: nanosecond timestamps, the tuple format, measuring elapsed time and common mistakes.

process.hrtime() returns a high-resolution timestamp from a monotonic clock, meaning one that only moves forward and is not affected by changes to the system date. The absolute value is meaningless (it counts from an arbitrary point in the past); only the difference between two readings is useful, which makes it ideal for measuring how long something took.

There are two forms. The classic process.hrtime() returns a two-element array [seconds, nanoseconds]. process.hrtime.bigint() returns the same moment as a single BigInt number of nanoseconds, which is easier to subtract.

NODE
const start = process.hrtime.bigint();
// ... work ...
const nanos = process.hrtime.bigint() - start;

Try it

Node.js Timing with bigint

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js The tuple form and diffs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
process.hrtime(time?: [number, number]): [number, number]
process.hrtime.bigint(): bigint
Name Description
time A tuple from an earlier hrtime() call. When given, the function returns the elapsed time since that reading.

Returns: [seconds, nanoseconds] where nanoseconds is between 0 and 999,999,999; or a BigInt of nanoseconds for hrtime.bigint().

Errors: ERR_INVALID_ARG_TYPE if time is not an array, and ERR_OUT_OF_RANGE if it does not have exactly two numbers.

Usage notes

  • Only differences mean anything. The raw value is not a date and not related to boot time or the Unix epoch.
  • Prefer hrtime.bigint() in new code: no tuple arithmetic, no float rounding. Convert with Number(diff) / 1e6 for milliseconds once the difference is small enough to fit in a double.
  • Never mix BigInt with Number. hrtime.bigint() - 5 throws a TypeError; use 5n.
  • Compared with performance.now(): that function returns fractional milliseconds from process start and is the web-standard choice; hrtime has integer nanoseconds and is Node-specific.
  • Do not use Date.now() to measure durations; it can jump when the clock is adjusted.
  • Resolution depends on the system; do not expect nanosecond accuracy of the actual work, and measure repeatedly.
  • Cheap but not free. For micro-benchmarks, run the code many times and use a proper benchmark tool.