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.