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?
The performance. now() method returns a DOMHighResTimeStamp , measured in milliseconds. The returned value represents the time elapsed since the time origin.
performance. now() has full support in all modern browsers, starting from Chrome 24, Firefox 15, and IE10.
The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With