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?
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?
Date.now()
returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC,performance.now()
returns the number of milliseconds, with microseconds in the fractional part, fromperformance.timing.navigationStart
, the start of navigation of the document, to theperformance.now()
call. Another important difference betweenDate.now()
andperformance.now()
is that the latter is monotonically increasing, so the difference between two calls will never be negative.For better understanding visit the link.
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
andnow
method (Internet Explorer) - MSDN for more information.The official W3C spec can be found here: High Resolution Time API