There are a number of 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 of the node-running environment, and the effect of the +new date ( ) is the same as the new Date (). GetTime ( ) effect.
In scenarios where you need to use timestamps frequently, you need to focus on the performance of the methods in which Date.now () is the best performance and can be tested 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 results:
"New Date (). GetTime ()" interval:1583
"+new Date" interval:2189
"Date.now ()" interval:891
If you're just getting a timestamp, then using date.now () is the best thing to do, but if you want to calculate the time difference, there's a problem with these methods: The system time of the operating environment is sometimes a minor callback, so the timing is inaccurate. Some bugs are sometimes raised.
Process.hrtime ()
This approach is based on the arbitrary taking of a past point of time, distance from the current time to get an accurate timestamp object: [Seconds, nanoseconds]
> process.hrtime ()
[3197146, 563552237]
This method has nothing to do with system time, so it will not be affected by the system clock drift, and it will not be a bug to calculate the time difference.
But there's always, but--
What if it's 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
No mistake, the same number of creation, the above Date.now () but about 900ms Ah!
Process.hrtime () is too slow, there are wood!!!
The original Nodejs processing high-precision time, the calculation is more complex, occupy more system resources, slow, 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 to get a time stamp of seconds by Nodejs the start run time, accurate to milliseconds:
Process.uptime
Input: 6.419
This function is based on node start time and is also not affected by system clock drift, which is suitable for calculating the timing.
So how about multiple invocation performance?
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
and Process.hrtime () compared to the performance of a lot of ~
No need to be so precise, just fast!
Then need high-frequency calculation time difference of the occasion, is you!
The above is Nodejs get time stamp and the whole content of difference, hope to everybody peacetime use Nodejs can help.