Performance - Date.now() vs Date.getTime()

javascript, timestamp

Solution

These things are the same (edit semantically; performance is a little better with `.now()`):

var t1 = Date.now();
var t2 = new Date().getTime();

However, the time value from any already-created `Date` instance is frozen at the time of its construction (or at whatever time/date it's been set to). That is, if you do this:

var now = new Date();

and then wait a while, a subsequent call to `now.getTime()` will tell the time at the point the variable was set.

Problem

``` var timeInMs = Date.now(); ``` per MDN vs. ``` var timeInMs = new Date(optional).getTime(); ``` per MDN. Is there any difference between the two, besides the syntax and the ability to set the Date (to not the current) via optional in the second version? `Date.now()` is faster - check out the jsperf

Original source