3 ms·
Interesting method to getting current timestamp: var I = +new Date; I've always used: var I = (new Date).getTime();
by loganlinn 16y ago
Interesting method to getting current timestamp:
var I = +new Date;
I've always used:
var I = (new Date).getTime();
- russ 16y agoThey're the same, +new Date doing an implicit cast to number. I prefer getTime as well for its clarity.
- loganlinn 16y agoAnd another form is: var I = Number(new Date);
- shazow 16y agoI benchmarked all the variations on Chrome, var foo = +new Date() was the fastest—not by a huge amount than the rest, but I remember there was one approach that was especially slow. I did this while optimizing performance of a fast-paced game in Javascript (LineRage). Profiling showed that getting timestamps was responsible for significant portion of the processing time. I also ended up switching to a global clock approach. Unfortunately I no longer have the original benchmark code/results. Would be happy to have someone replicate it.
- altano 16y agoThere's also the equivalent Date.now() (as of ES5). If you'd like to write test code that overrides the current Date, this is the easiest version to override. Monkey-patching Date's constructor is a disaster. Plus, backwards compatibility can easy be added (see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/now https://developer.mozilla.org/en/JavaScript/Reference/Global...) to non-ES5-compliant browsers.