performance.now() 方法返回 DOMHighResTimeStamp , measured in milliseconds.

注意: 此特征可用于 Web 工作者 .

The returned value represents the time elapsed since the time origin .

Bear in mind the following points:

  • In dedicated workers created from a 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.
  • In shared or service workers, the value in the worker might be higher than that of the main context because that window can be created after those workers.

It's important to keep in mind that to mitigate potential security threats such as Spectre , browsers typically round the returned value by some amount in order to be less predictable. This inherently introduces a degree of inaccuracy by limiting the resolution or precision of the timer. For example, Firefox rounds the returned time to 1 millisecond increments.

The precision of the returned value is subject to change if/when the security concerns are alleviated through other means.

句法

t = performance.now();
				

范例

const t0 = performance.now();
doSomething();
const 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() .

Reduced time precision

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.

Starting with Firefox 79, high resolution timers can be used if you cross-origin isolate your document using the Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy headers:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
				

These headers ensure a top-level document does not share a browsing context group with cross-origin documents. COOP process-isolates your document and potential attackers can't access to your global object if they were opening it in a popup, preventing a set of cross-origin attacks dubbed XS-Leaks .

规范

规范 状态 注释
High Resolution Time Level 2
The definition of 'performance.now()' in that specification.
推荐 Stricter definitions of interfaces and types.
高分辨率时间
The definition of 'performance.now()' in that specification.
推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
now Chrome 24
24
不支持 21 — 24 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 15
15
In Firefox 57.0.4 the accuracy was reduced to 20 microseconds.
In Firefox 59 the accuracy was reduced to 2 milliseconds.
In Firefox 60 the accuracy was increased to 1 millisecond.
IE 10 Opera 15 Safari 8 WebView Android Yes Chrome Android 25 Firefox Android 15
15
In Firefox 57.0.4 the accuracy was reduced to 20 microseconds.
In Firefox 59 the accuracy was reduced to 2 milliseconds.
In Firefox 60 the accuracy was increased to 1 millisecond.
Opera Android 14 Safari iOS 9 Samsung Internet Android 1.5

图例

完整支持

完整支持

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: