这篇翻译不完整。请帮忙从英语翻译这篇文章

performance.now()方法返回一个精确到毫秒的  DOMHighResTimeStamp

The timestamp is not actually high-resolution. To mitigate security threats such as Spectre, browsers currently round the result to varying degrees. (Firefox started rounding to 2 milliseconds in Firefox 59.) 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.

Note: 此特性在 Web Worker 中可用。

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.

语法

const t = window.performance.now();

示例

let t0 = window.performance.now();
doSomething();
let t1 = window.performance.now();
console.log("doSomething函数执行了" + (t1 - t0) + "毫秒.")

和JavaScript中其他可用的时间类函数(比如Date.now)不同的是,window.performance.now()返回的时间戳没有被限制在一毫秒的精确度内,而它使用了一个浮点数来达到微秒级别的精确度。

另外一个不同点是,window.performance.now()是以一个恒定的速率慢慢增加的,它不会受到系统时间的影响(可能被其他软件调整)。另外,performance.timing.navigationStart + performance.now() 约等于 Date.now()

规范

Specification Status Comment
High Resolution Time Level 2
performance.now()
Candidate Recommendation Stricter definitions of interfaces and types.
High Resolution Time
performance.now()
Recommendation Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 24Edge Full support YesFirefox Full support 15
Notes
Full support 15
Notes
Notes In Firefox 57.0.4 the accuracy was reduced to 20 microseconds.
Notes In Firefox 59 the accuracy was reduced to 2 milliseconds.
IE Full support 10Opera Full support 15Safari Full support 8WebView Android Full support YesChrome Android Full support 25Edge Mobile Full support YesFirefox Android Full support 15
Notes
Full support 15
Notes
Notes In Firefox 57.0.4 the accuracy was reduced to 20 microseconds.
Notes In Firefox 59 the accuracy was reduced to 2 milliseconds.
Opera Android No support NoSafari iOS Full support 9Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

相关链接

文档标签和贡献者

此页面的贡献者: zhangchen, dblate, xgqfrms-GitHub, teoli, colder, ziyunfei
最后编辑者: zhangchen,