Performance: now() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

Note: This feature is available in Web Workers.

The performance.now() method returns a high resolution timestamp in milliseconds. It represents the time elapsed since Performance.timeOrigin (the time when navigation has started in window contexts, or the time when the worker is run in Worker and ServiceWorker contexts).

Syntax

js
now()

Parameters

None.

Return value

Returns a DOMHighResTimeStamp measured in milliseconds.

Description

Performance.now vs. Date.now

Unlike Date.now, the timestamps returned by performance.now() are not limited to one-millisecond resolution. Instead, they represent times as floating-point numbers with up to microsecond precision.

Also, Date.now() may have been impacted by system and user clock adjustments, clock skew, etc. as it is relative to the Unix epoch (1970-01-01T00:00:00Z) and dependent on the system clock. The performance.now() method on the other hand is relative to the timeOrigin property which is a monotonic clock: its current time never decreases and isn't subject to adjustments.

performance.now specification changes

The semantics of the performance.now() method changed between High Resolution Time Level 1 and Level 2.

Changes Level 1 Level 2
Relative to performance.timing.navigationStart Performance.timeOrigin
Triggering conditions Document fetch or unload prompt (if any). Creation of the browsing context (if no prior document), unload prompt (if any), or start of the navigation (as defined in HTML, a few steps before fetch).

The performance.now() method used to be relative to performance.timing.navigationStart property from the Navigation Timing specification. This changed and performance.now() is now relative to Performance.timeOrigin which avoids clock change risks when comparing timestamps across webpages.

js
// Level 1 (clock change risks)
currentTime = performance.timing.navigationStart + performance.now();

// Level 2 (no clock change risks)
currentTime = performance.timeOrigin + performance.now();

Ticking during sleep

The specification (Level 2) requires that performance.now() should tick when the operating system sleeps or the browser process freezes in any other way. It appears that only browsers on Windows keep ticking during sleep. Relevant browser bugs for other operating systems:

  • Chrome/Chromium (bug)
  • Firefox (bug)
  • Safari/WebKit (bug)

Depending on your use case, this divergence may or may not be significant. For example, if you are timing short operations such as loading an image, during which time the system is unlikely to sleep, this may not cause any issues. If you are timing a long operation, you may find Date.now() more useful to avoid these limitations, since the high precision of performance.now() may not be as critical anyway.

More details can also be found in the specification issue hr-time#115.

Security requirements

To offer protection against timing attacks and fingerprinting, performance.now() is coarsened based on whether or not the document is cross-origin isolated.

  • Resolution in isolated contexts: 5 microseconds
  • Resolution in non-isolated contexts: 100 microseconds

You can use the Window.crossOriginIsolated and WorkerGlobalScope.crossOriginIsolated properties to check if the document is cross-origin isolated:

js
if (crossOriginIsolated) {
  // Use measureUserAgentSpecificMemory
}

Examples

Using performance.now()

To determine how much time has elapsed since a particular point in your code, you can do something like this:

js
const t0 = performance.now();
doSomething();
const t1 = performance.now();
console.log(`Call to doSomething took ${t1 - t0} milliseconds.`);

Specifications

Specification
High Resolution Time
# dom-performance-now

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
now
Chrome – No support
Chrome 21 – 23 (Release date: 2012-07-31)
prefix
prefix Implemented with the vendor prefix: webkit
Chrome – Full support
Chrome 24 (Release date: 2013-01-10)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 15 (Release date: 2012-08-28)
footnote
footnote In Firefox 57.0.4 the accuracy was reduced to 20 microseconds.
footnote In Firefox 59 the accuracy was reduced to 2 milliseconds.
footnote In Firefox 60 the accuracy was increased to 1 millisecond.
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 8 (Release date: 2014-10-16)
footnote Full support
Chrome Android – Full support
Chrome Android 25 (Release date: 2013-02-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 15 (Release date: 2012-08-28)
footnote
footnote In Firefox for Android 57.0.4 the accuracy was reduced to 20 microseconds.
footnote In Firefox for Android 59 the accuracy was reduced to 2 milliseconds.
footnote In Firefox for Android 60 the accuracy was increased to 1 millisecond.
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1.5 (Release date: 2013-09-25)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 8.5 (Release date: 2017-09-12)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also