performance.now() vs Date.now()

2019-01-14 10:28发布

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?

2条回答
We Are One
2楼-- · 2019-01-14 11:00

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, from performance.timing.navigationStart, the start of navigation of the document, to the performance.now() call. Another important difference between Date.now() and performance.now() is that the latter is monotonically increasing, so the difference between two calls will never be negative.

For better understanding visit the link.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-14 11:08

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

查看更多
登录 后发表回答