Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monotonically increasing time in Node.js

This question has already been answered for the browser here, but window.performance.now() is obviously not available in Node.js.

Some applications need a steady clock, i.e., a clock that monotonically increases through time, not subject to system clock drifts. For instance, Java has System.nanoTime() and C++ has std::chrono::steady_clock. Is such clock available in Node.js?

like image 434
Lucio Paiva Avatar asked Oct 26 '17 22:10

Lucio Paiva


2 Answers

Turns out the equivalent in Node.js is process.hrtime(). As per the documentation:

[The time returned from process.hrtime() is] relative to an arbitrary time in the past, and not related to the time of day and therefore not subject to clock drift.


Example

Let's say we want to periodically call some REST endpoint once a second, process its outcome and print something to a log file. Consider the endpoint may take a while to respond, e.g., from hundreds of milliseconds to more than one second. We don't want to have two concurrent requests going on, so setInterval() does not exactly meet our needs.

One good approach is to call our function one first time, do the request, process it and then call setTimeout() and reschedule for another run. But we want to do that once a second, taking into account the time we spent making the request. Here's one way to do it using our steady clock (which will guarantee we won't be fooled by system clock drifts):

function time() {
    const nanos = process.hrtime.bigint();
    return Number(nanos / 1_000_000n);
}

async function run() {
    const startTime = time();

    const response = await doRequest();
    await processResponse(response);

    const endTime = time();
    // wait just the right amount of time so we run once second; 
    // if we took more than one second, run again immediately
    const nextRunInMillis = Math.max(0, 1000 - (endTime - startTime));
    setTimeout(run, nextRunInMillis);
}

run();

I made this helper function time() which converts the value returned by process.hrtime.bigint() to a timestamp with milliseconds resolution; just enough resolution for this application.

like image 80
Lucio Paiva Avatar answered Nov 16 '22 05:11

Lucio Paiva


NodeJS 10.7.0 added process.hrtime.bigint().

You can then do this:


function monotimeRef() {
  return process.hrtime.bigint();
}

function monotimeDiff(ref) {
  return Number(process.hrtime.bigint() - ref) / 10**9;
}

Demonstrating the usage in a Node REPL:

// Measure reference time.
> let t0 = monotimeRef();
undefined

[ ... let some time pass ... ]

// Measure time passed since reference time,
// in seconds.
> monotimeDiff(t0)
12.546663115

Note:

  • Number() converts a BigInt to a regular Number type, allowing for translating from nanoseconds to seconds with the normal division operator.
  • monotimeDiff() returns the wall time difference passed with nanosecond resolution as a floating point number (as of converting to Number before doing the division).
  • This assumes that the measured time duration does not grow beyond 2^53 ns, which is actually just about 104 days (2**53 ns / 10**9 ns/s / 86400.0 s/day = 104.3 day).
like image 36
Dr. Jan-Philip Gehrcke Avatar answered Nov 16 '22 05:11

Dr. Jan-Philip Gehrcke