126

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?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Lewis
  • 14,132
  • 12
  • 66
  • 87
  • 27
    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*. Basically, `performance.now()` should only be used when you want to measure the relative distance between two time points, not their "absolute" position in time. – user703016 Jun 12 '15 at 04:59
  • 3
    What @buttifulbuttefly says, plus ... `performance.now` offers more precise timing (sub-millisecond precision). – markE Jun 12 '15 at 04:59
  • 5
    Indeed, more precise *timing*, not more precise *time*. – Amadan Jun 12 '15 at 05:00
  • 4
    @markE This is not true anymore. Due to [Spectre](https://spectreattack.com/), the precision of `performance.now` [is being limited](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now) – zypA13510 Aug 06 '18 at 06:39
  • **Compare To**: [Date.now() vs Date.getTime()](https://stackoverflow.com/q/12517359/1366033) – KyleMit Dec 07 '21 at 16:10

3 Answers3

111

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

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • 11
    That precision comes at a cost. It's 50% slower. Good to know if you need it in a tight loop. https://jsperf.com/perf-vs-date/1 – Eyal May 04 '17 at 06:42
  • 3
    Oops, I meant 80% slower. :-( – Eyal May 04 '17 at 07:47
  • 5
    Keep in mind what google audits think about date.now() https://developers.google.com/web/tools/lighthouse/audits/date-now – StLia Oct 19 '17 at 09:08
  • 4
    currently in Firefox performance.now() is like 20% faster but in other browsers suprasingly slower like 60% - you can check for yourselfs: https://jsbench.me/s6jz9v29i3/1 – Picard Aug 13 '19 at 13:35
  • 1
    3x slower on my Raspberry Firefox. performance.now() seems not performant but is more precise. A few hundred measures will not affect performance anyway. –  Oct 30 '19 at 15:33
  • 1
    @StLia what do they think? That links actually points back here. – Pithikos Jun 16 '20 at 09:11
  • 21
    why does developers.google.com/web/tools/lighthouse/audits/date-now redirect to this stackoverflow question – Muhammad Umer Dec 03 '20 at 17:36
  • It's only **10x** more accurate: see my post at https://stackoverflow.com/a/70545051/15012953 – Infigon Jan 29 '22 at 23:48
  • Practically, it's not any more accurate. performance.now gets rounded to millisecond precision in [both Firefox](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now) and [WebKit](https://trac.webkit.org/changeset/226495/webkit), so unless you're developing two different codebases, one for Chrome, and one for FF/Safari. – Mike 'Pomax' Kamermans Mar 30 '22 at 04:08
31

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.

GetFree
  • 40,278
  • 18
  • 77
  • 104
Dreamweaver
  • 1,328
  • 11
  • 21
10

The first thing I noticed was that performance.now() is 4 times slower than Date.now() (400k operations vs 100k on my computer). However, if you want accurate timing/time since page load, using performance.now() is the better option. 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 tenths of a millisecond instead of milliseconds.

As for support, Date.now() has slightly more support than performance.now(), as both are supported by modern browsers, and even Internet Explorer 10 and 11.

new Date().getTime() has just a bit more support and is 2x slower than Date.now(). It's slower, because it creates a big object, but only calls a single function.

However, this caniuse.com page shows that performance.now() is almost always okay, in 98.25% (as of right now) of cases.

(From here on out I will only refer to Date.now(), but new Date.getTime() is the same in the cases below)

Usage

Date.now() can (and should) be used for delta time (particularly requestAnimationFrame(): it can easily get the correct frame on a 120fps display (and is twice as speedy)! It can also be used for clocks. It counts how many milliseconds have passed since the Unix Epoch (see the top answer for more details). For applications that require a bit more accuracy (see below), performance.now() can be used. It behaves the exact same as Date.now() if you're using a timer (because it still counts up in milliseconds), except it's more accurate. To use performance.now() as a (slightly) more accurate Date.now() or object, add performance.timing.navigationStart to the value. This does seem to be a few milliseconds off from the Date.now() object, but as to why I'm not sure. Accuracy

From what I can tell, performance.now() is only 10x more accurate compared to Date.now() on my Chrome desktop: considering time leaps of 0.1ms, However, this accuracy is good enough for most time-strict applications. (Read the Firefox section for more info on their accuracy.) It may look more accurate (like 313015.59999999404), but it's not: it is intentionally limited for security reasons, because the exploitation of time could allow malicious code to access other applications.

Firefox

Sadly, this performance (of 0.1ms) is not possible if you have Firefox users and do not have access to Cross-Origin headers. Instead, you can only get an accuracy of 2ms (also the same as Date.now()!) In order to enable it, put the Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp in your document. Users can also increase this speed to 100ms (or higher). Visit the Mozilla documentation for more information.

Infigon
  • 554
  • 5
  • 18
  • 3
    This is not a floating point issue, precision is [intentionally limited now to mitigate Spectre and other issues](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now) – pfg Feb 17 '22 at 19:18