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, you can use these methods. So what is the difference between these methods?
new Date().getTime() and Date.now()
These methods are through the number of milliseconds of the system running environment in the node. The effect of +new Date() writing method is the same as that of new Date().getTime() .
In scenarios where timestamps are frequently used, you need to pay attention to the performance of the method. Among these methods, Date.now() has the best performance. You can test it with a little 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 just get the timestamp, then using Date.now() is the best way to do it, but if you want to calculate the time difference, these methods will have some problems: sometimes there will be slight callbacks in the system time of the running environment, so the time difference obtained is inaccurate and sometimes some bugs will be triggered.
process.hrtime()
This method is to obtain an exact timestamp object based on any past time point taken and the time from the present: [seconds, nanoseconds]
> process.hrtime()[ 3197146, 563552237 ]
This method has nothing to do with system time, so it will not be affected by system clock drift, and there will be no bugs when used to calculate the time difference.
But, everything is always there - -
What if it is used in a place where it is called frequently?
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 same number of creations, the Date.now() above is about 900ms!
process.hrtime() is too slow! ! !
It turns out that when nodejs handles high-precision time, the calculation is relatively complicated, occupies a lot of system resources, and has a slow speed, so this method is not suitable for high-frequency applications. Please see process.uptime() below
process.uptime()
This function uses nodejs startup run time to get a second timestamp, which is accurate to milliseconds:
process.uptime
Input: 6.419
This function is based on the node startup time and will not be affected by the system clock drift, and is suitable for calculating the time difference.
So how 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(), it creates a lot of performance~
Don't count so accurately, it's fast!
Then you need to calculate the time difference at high frequency!
The above is all about nodejs getting the difference between the timestamp and the time. I hope it will be helpful to everyone when using nodejs.