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 inWeb Workers.
Theperformance.now() method returns a high resolution timestamp in milliseconds. It represents the time elapsed sincePerformance.timeOrigin (the time when navigation has started in window contexts, or the time when the worker is run inWorker andServiceWorker contexts).
In this article
Syntax
now()Parameters
None.
Return value
Returns aDOMHighResTimeStamp measured in milliseconds.
Description
>Performance.now vs.Date.now
UnlikeDate.now, the timestamps returned byperformance.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.Theperformance.now() method on the other hand is relative to thetimeOrigin property which is amonotonic clock: its current time never decreases and isn't subject to adjustments.
performance.now specification changes
The semantics of theperformance.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). |
Theperformance.now() method used to be relative toperformance.timing.navigationStart property from the Navigation Timing specification. This changed andperformance.now() is now relative toPerformance.timeOrigin which avoids clock change risks when comparing timestamps across webpages.
// 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 thatperformance.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:
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 findDate.now() more useful to avoid these limitations, since the high precision ofperformance.now() may not be as critical anyway.
More details can also be found in the specification issuehr-time#115.
Security requirements
To offer protection against timing attacks andfingerprinting,performance.now() is coarsened based on whether or not the document iscross-origin isolated.
- Resolution in isolated contexts: 5 microseconds
- Resolution in non-isolated contexts: 100 microseconds
You can use theWindow.crossOriginIsolated andWorkerGlobalScope.crossOriginIsolated properties to check if the document is cross-origin isolated:
if (crossOriginIsolated) { // Use measureUserAgentSpecificMemory}Examples
>Usingperformance.now()
To determine how much time has elapsed since a particular point in your code, you can do something like this:
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> |