Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

People also ask

Is performance now faster than Date now?

No! Date. now() returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, performance. now() returns the number of milliseconds/microseconds elapsed since an arbitrary epoch.

What does Date getTime () do?

Javascript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00. You can use this method to help assign a date and time to another Date object.

What does performance now () return?

now() The performance. now() method returns a DOMHighResTimeStamp , measured in milliseconds. The returned value represents the time elapsed since the time origin.

Is Date now slow?

It was a real boost for Internet Explorer, Firefox and Opera, but made it slower for Chrome, where (together with Opera) the Date. now() is the fastest.


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.


They are effectively equivalent, but you should use Date.now(). It's clearer and about twice as fast.

Edit: Source: http://jsperf.com/date-now-vs-new-date


When you do (new Date()).getTime() you are creating a new Date object. If you do this repeatedly, it will be about 2x slower than Date.now()

The same principle should apply for Array.prototype.slice.call(arguments, 0) vs [].slice.call(arguments, 0)


Yes, that is correct; they are effectively equivalent when using the current time.


Sometimes it's preferable to keep some time tracking variable in a Date object format rather than as just a number of milliseconds, to have access to Date's methods without re-instantiating. In that case, Date.now() still wins over new Date() or the like, though only by about 20% on my Chrome and by a tiny amount on IE.

See my JSPERF on

timeStamp2.setTime(Date.now()); // set to current;

vs.

timeStamp1 = new Date(); // set to current;

http://jsperf.com/new-date-vs-settime