How does nodejs obtain the timestamp and time difference?

Source: Internet
Author: User
Tags time in milliseconds
This article describes in detail multiple methods for obtaining Timestamp and time difference from nodejs, which is helpful for the use of nodejs at ordinary times. Let's take a look at it. There are many methods to obtain the timestamp in Nodejs, such:

1. new Date (). getTime ()

2. Date. now ()

3. process. uptime ()

4. process. hrtime ()

You can use these methods to obtain a timestamp. What are the differences between these methods?

New Date (). getTime () and Date. now ()

These methods use the node runtime environment's system time in milliseconds. The + new Date () method works the same way as new Date (). getTime.

In scenarios where timestamp is frequently used, you need to pay attention to the method performance. In these methods, Date. now () has the best performance. You can test it with a bit of 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 result:

[New Date (). getTime ()] interval: 1583

[+ New Date] interval: 2189

[Date. now ()] interval: 891

If you only get the timestamp, use Date. now () is the best practice, but if you want to calculate the time difference, these methods will be a bit of a problem: the system time of the runtime environment sometimes has a tiny callback, the time difference is inaccurate, and sometimes some bugs are triggered.

Process. hrtime ()

This method obtains an exact timestamp object based on an arbitrary past time point from the current 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 bugs when it is used to calculate the time difference.

However, everything always works, --

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, the number of creation times is the same. The Date. now () above is about Ms!

Process. hrtime () is too slow !!!

In the past, when nodejs processed high-precision time, the calculation was complicated, occupying a lot of system resources, and the speed was slow. Therefore, this method is not suitable for high-frequency applications. See process. uptime ()

Process. uptime ()

This function obtains a timestamp in seconds through the start and run time of node. JS, accurate to milliseconds:

Process. uptime

Input: 6.419

This function is based on the start time of the node and is not affected by the system clock drift. It is suitable for calculating the 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 (), performance is much better ~

You don't need to be so precise. It's just fast!

You need to calculate the time difference at a high frequency!

The above is all about the time stamp and time difference obtained by nodejs. I hope it will be helpful when you use nodejs.

For more articles about how to obtain the timestamp and time difference in nodejs, refer to PHP!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.