Home > Back-end >  Node process.hrtime() is five years off?
Node process.hrtime() is five years off?

Time:12-03

I'm using Node v16.17 on MacBook Pro M1. I want to use microsecond timestamps, so I tried process.hrtime(). But this is very strange, as the first array element (which should be seconds when multiplied by 1000) is like some date in 2017:

> new Date().getTime();
1669997280728
> process.hrtime();
[ 1486038, 90680583 ]

So, if I take 1486038000 --> it is Thu, 02 Feb 2017 12:20:00 GMT If I take out the milliseconds from new Date().getTime() -> it is correctly Fri, 02 Dec 2022 16:08:00 GMT

What it the issue here? I thought process.hrtime() will be the high resolution time, but why is this so off?

Thanks Fritz

CodePudding user response:

Per the docs,

These times are relative to an arbitrary time in the past, and not related to the time of day and therefore not subject to clock drift. The primary use is for measuring performance between intervals

https://nodejs.org/api/process.html#processhrtimetime

It is only a coincidence that you got a somewhat relevant date.

You should be using process.hrtime.bigint(), however, because process.hrtime() has been for a while (even in Node v16.17).

CodePudding user response:

What?

process.hrtime() has nothing do to with the real-time clock, as is explained by the docs:

These times are relative to an arbitrary time in the past, and not related to the time of day and therefore not subject to clock drift.

(emphasis mine)

And,

The primary use is for measuring performance between intervals:

  • Related