The performance.now()
method returns a DOMHighResTimeStamp
, measured in milliseconds.
The timestamp is not actually high-resolution. To mitigate security threats such as Spectre, browsers currently round the results to varying degrees. (Firefox started rounding to 1 millisecond in Firefox 60.) Some browsers may also slightly randomize the timestamp. The precision may improve again in future releases; browser developers are still investigating these timing attacks and how best to mitigate them.
The returned value represents the time elapsed since the time origin.
Bear in mind the following points:
Window
context, the value in the worker will be lower than performance.now()
in the window who spawned that worker. It used to be the same as t0
of the main context, but this was changed.t = performance.now();
var t0 = performance.now(); doSomething(); var t1 = performance.now(); console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
Unlike other timing data available to JavaScript (for example 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 unlike Date.now()
, the values returned by Performance.now()
always increase at a constant rate, independent of the system clock (which might be adjusted manually or skewed by software like NTP). Otherwise, performance.timing.navigationStart + performance.now()
will be approximately equal to Date.now()
.
To offer protection against timing attacks and fingerprinting, the precision of performance.now()
might get rounded depending on browser settings.
In Firefox, the privacy.reduceTimerPrecision
preference is enabled by default and defaults to 1ms.
// reduced time precision (1ms) in Firefox 60 performance.now(); // 8781416 // 8781815 // 8782206 // ... // reduced time precision with `privacy.resistFingerprinting` enabled performance.now(); // 8865400 // 8866200 // 8866700 // ...
In Firefox, you can also enable privacy.resistFingerprinting
— this changes the precision to 100ms or the value of privacy.resistFingerprinting.reduceTimerPrecision.microseconds
, whichever is larger.
Specification | Status | Comment |
---|---|---|
High Resolution Time Level 2 The definition of 'performance.now()' in that specification. | Candidate Recommendation | Stricter definitions of interfaces and types. |
High Resolution Time The definition of 'performance.now()' in that specification. | Recommendation | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 24 | Yes | 15
|
10 | 15 | 8 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | 25 | Yes | 15
|
No | 9 | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Performance/now