Date() vs Date().getTime()

date, javascript

Solution

I get that it wasn't in your questions, but you may want to consider `Date.now()` which is fastest because you don't need to instantiate a new `Date` object, see the following for a comparison of the different versions: http://jsperf.com/date-now-vs-new-date-gettime/8

The above link shows using `new Date()` is faster than `(new Date()).getTime()`, but that `Date.now()` is faster than them all.

Browser support for `Date.now()` isn't even that bad (IE9+):

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/now

Problem

What is the difference between using `new Date()` and `new Date().getTime()` when subtracting two timestamps? (test script on jsFiddle) Both of the following gives the same results: ``` var prev1 = new Date(); setTimeout(function() { var curr1 = new Date(); var diff1 = curr1 - prev1; }, 500); var prev2 = new Date().getTime(); setTimeout(function() { var curr2 = new Date().getTime(); var diff2 = curr2 - prev2; }, 500); ``` Is there a reason I should prefer one over another?

Original source

Related problems