[From] http://www.jb51.net/article/89767.htm
There are many ways to get timestamps in Nodejs, such as:
1.new Date (). GetTime ()
2.Date.now ()
3.process.uptime ()
4.process.hrtime ()
Usually want to get a time stamp, with these methods can be, then what is the difference between these methods?
New Date (). GetTime () and Date.now ()
These methods are the system time milliseconds through node's running environment, and the effect of +new date ( ) is the same as the new Date (). GetTime ().
In scenarios where frequent timestamps are required, it is important to focus on the performance of the methods in which Date.now () performs best and can be tested with a bit of code:
?
1234567891011121314151617181920212223242526272829 |
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
Using Date.now () is the best way to get a timestamp, but if you want to calculate the time difference, there are some problems with these methods: The system time of the operating environment sometimes has a small callback, so the difference in timing is inaccurate, sometimes causing some bugs.
Process.hrtime ()
This approach is based on an arbitrary fetch of a past point in time, distance from the present time to obtain an exact timestamp object: [sec, nanosecond]
?
12 |
> process.hrtime() [ 3197146, 563552237 ] |
This method is independent of the system time, so it is not affected by the system clock drift, and there is no bug when calculating the time difference.
But everything is always there, but--
What if it is used in a place that is frequently called?
?
123456789101112131415 |
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
Remember correctly, the same number of creation, the above Date.now () is about 900ms Ah!
Process.hrtime () is too slow to have wood there!!!
The original NODEJS processing high-precision time, the calculation is more complex, the use of system resources, slow speed, then in the high-frequency application of the place is not suitable for this method. Below please see process.uptime ()
Process.uptime ()
This function is started by Nodejs to get a time stamp of seconds, accurate to milliseconds:
Process.uptime
Input: 6.419
This function is based on the node start time and is also not affected by the system clock drift, and is suitable for calculating the timing difference.
What about the performance of multiple invocations?
?
123456789101112 |
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++) { &NBSP;&NBSP; t2 = Process.uptime () *1000; &NBSP;&NBSP; //interval = (T2-T1); } interval = (T2-T1); console.log ( "Process.uptime ()" Interval: ' |
Output: "Process.uptime ()" interval:954
and Process.hrtime () compared to the performance of a lot of it ~
Don't be so precise, just hurry!
Then the need for high-frequency computing time difference, it is you!
The above is Nodejs get timestamp and time difference of the whole content, hope to everyone usually use nodejs time can be helpful.
[Go] node. js How to get timestamp and time difference