I have code like that sequentially:
var d0 = new Date();
...
...
var d1 = new Date();
if (d0>d1) {
console.log("Problem");
}
first look at it, it is no way d1 older than d0. But I just experienced that 2 days ago in a production server. d1 is older than d0 by few milliseconds. How can it be?
and what is the Date() coming from? is it from V8? or from OS system?
It'd be hard to say without taking a much closer look at your system, but in general remember that programming with date and time libraries can occasionally be really, really strange.
Recommended reading: Falsehoods Programmers believe about time
I recently experienced a similar issue in v8 on a production box, and what (we think) was happening was the network time daemon was changing the system's clock -- as it's designed to do. So, very occasionally we had some profiling that said our "end time" came before our "start time" because the system clock was getting re-synced by NTP in the middle of the code execution.
Many servers use some method of synchronizing their system time to some higher authority (using NTP or some other protocol). This synchronizing is done periodically and can cause the time to move backwards if that's necessary to bring the system's clock back in sync. This is done asynchronously to your own code so it could happen between the those two new Date()
calls.
The value of new Date()
is ultimately coming from the OS that V8 is calling into to get the time.