Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performance.now() vs Date.now()

What is the difference between performance.now() and Date.now()?

Should I consider performance.now() as a replacement for Date.now() since performace.now() is more consistent and independent?

like image 297
Lewis Avatar asked Jun 12 '15 04:06

Lewis


People also ask

What does performance now () return?

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

Can I use performance now?

performance. now() has full support in all modern browsers, starting from Chrome 24, Firefox 15, and IE10.

What does date now () produce?

The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

Is performance now faster than date now?

It's purely dependent on the time since the code started running, and clock changes do not affect the time. It's also more accurate: counting us (microseconds) instead of ms. As for support, Date. now() has slightly more support than performance.


1 Answers

They both serve different purposes.

performance.now() is relative to page load and more precise in orders of magnitude. Use cases include benchmarking and other cases where a high-resolution time is required such as media (gaming, audio, video, etc.)

It should be noted that performance.now() is only available in newer browsers (including IE10+).

Date.now() is relative to the Unix epoch (1970-01-01T00:00:00Z) and dependent on system clock. Use cases include same old date manipulation ever since the beginning of JavaScript.

See When milliseconds are not enough: performance.now and now method (Internet Explorer) - MSDN for more information.

The official W3C spec can be found here: High Resolution Time API

like image 119
rink.attendant.6 Avatar answered Sep 21 '22 22:09

rink.attendant.6