nodejs How to Get Time Stamps and Time Differences

  • 2021-07-07 06:23:37
  • OfStack

There are many ways to get timestamps in Nodejs, such as:

1.new Date().getTime()

2.Date.now()

3.process.uptime()

4.process.hrtime()

If you want to get a timestamp at ordinary times, you can use these methods, so what's the difference between these methods?

new Date (). getTime () and Date. now ()

These methods are based on the system time milliseconds of the node running environment, and the + new Date () writing has the same effect as new Date (). getTime ().

In scenarios where timestamps need to be used frequently, you need to pay attention to method performance. Among these methods, Date. now () has the best performance and can be tested with 1 point code:


var t1 = new Date().getTime();
var t2 = t1;
var i = 0, count = 10000000, interval = 0;

for(i = 0; i < count; i++)
{
  t2 = new Date().getTime();
  interval = (t2 - t1);
}
interval = (t2 - t1);
console.log(' " new Date().getTime() " interval: ', interval);

t1 = new Date().getTime();
for(i = 0; i < count; i++)
{
  t2 = +new Date;
  interval = (t2 - t1);
}
interval = (t2 - t1);
console.log(' " +new Date " interval: ', interval);

t1 = new Date().getTime();
for(i = 0; i < count; i++)
{
  t2 = Date.now();
  interval = (t2 - t1);
}
interval = (t2 - t1);
console.log(' " Date.now() " interval: ', interval);

Output:

"new Date (). getTime ()" interval: 1583

"+ new Date" interval: 2189

"Date. now ()" interval: 891

Using Date. now () is the best practice if you just get the timestamp, but if you want to calculate the time difference, these methods will have some problems: the system time of the running environment sometimes has a slight callback, which makes the resulting time difference inaccurate and sometimes triggers some BUG.

process.hrtime()

In this way, an accurate timestamp object is obtained according to an arbitrary past time point and the distance from the present time: [seconds, nanoseconds]


> process.hrtime()
[ 3197146, 563552237 ]

This method has nothing to do with the system time, so it will not be affected by the system clock drift, and there will be no BUG when calculating the time difference.

But, there's always a buts--

What if it is used in a place that is frequently called?


var t1 = new Date().getTime();
var t2 = t1;
var i = 0, count = 10000000, interval = 0;

var hrTime1 = process.hrtime();
var hrTime2 = hrTime1;

t1 = new Date().getTime();
for(i = 0; i < count; i++)
{
  hrTime2 = process.hrtime(hrTime1);
}
t2 = new Date().getTime();
interval = parseInt(hrTime2[0] * 1e3 + hrTime2[1] * 1e-6);
console.log(' " hrTime " interval: ', interval, t2 - t1);

【hrTime】interval: 6412 6413 If you remember correctly, with the same number of times, the above Date. now () is about 900ms!

process. hrtime () is too slow! ! !

It turns out that nodejs is complicated in calculation, takes up more system resources and is slow in speed when dealing with high-precision time, so it is not suitable for this method in high-frequency applications. See process. uptime () below

process. uptime ()

This function gets a timestamp of 1 second through nodejs startup time, which is accurate to milliseconds:

process.uptime

Input: 6.419

This function is based on the start-up time of node, and is also not affected by system clock drift, so it is suitable for calculating time difference.

So what is the performance of multiple calls?


var t1 = new Date().getTime();
var t2 = t1;
var i = 0, count = 10000000, interval = 0;

t1 = process.uptime()*1000;
for(i = 0; i < count; i++)
{
  t2 = process.uptime()*1000;
  //interval = (t2 - t1);
}
interval = (t2 - t1);
console.log(' " process.uptime() " interval: ', interval);

Output: "process. uptime ()" interval: 954

Compared with process. hrtime (), the performance is much better ~

It doesn't have to be so accurate, it's fast!

Then you are the one who needs to calculate the time difference at high frequency!

The above is the whole content of nodejs to obtain timestamp and time difference. I hope it can be helpful for everyone to use nodejs at ordinary times.


Related articles: