JavaScript中的微时序 [英] Microsecond timing in JavaScript

查看:299
本文介绍了JavaScript中的微时序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中是否有任何具有微秒分辨率的计时功能?



我知道 timer.js ,我希望有其他友好浏览器的解决方案,如Firefox,Safari,Opera,Epiphany,Konqueror等。我对此不感兴趣支持任何IE浏览器,但欢迎回答包括 IE。



(鉴于JS中毫秒级的准确性不佳,我没有拿着我的在这一个上呼吸!)



更新:timer.js通告微秒分辨率,但它只是将毫秒读数乘以1,000。通过测试和代码检查验证。失望。 :[

解决方案

正如Mark Rejhon的回答所提到的那样,现代浏览器中有一种API可以暴露亚毫秒级的分辨率时序数据到脚本: W3C高分辨率计时器,又名窗口.performance.now()



now()优于传统 Date.getTime()有两个重要方面:


  1. now()是一个具有亚毫秒分辨率的双精度,表示自页面导航开始以来的毫秒数。它返回小数中的微秒数(例如,1000.123的值是1秒和123微秒)。


  2. now() 单调增加。这很重要,因为 Date.getTime()可以可能在后续调用中向前跳转甚至向后跳转。值得注意的是,如果操作系统的系统时间更新(例如原子钟同步),则还会更新 Date.getTime() now()保证始终单调增加,因此不受操作系统系统时间的影响 - 它始终是挂钟时间(假设你的挂钟是不是原子的......)。


now()几乎每个地方都可以使用 new Date.getTime() + new Date Date.now()是。例外情况是日期 now()次不混合,因为日期基于 unix-epoch (自1970年以来的毫秒数),而 now()是自页面导航开始以来的毫秒数(因此它将远小于日期) Chrome稳定版,Firefox 15+版和IE10版支持

now()。还有几个 polyfills


Are there any timing functions in JavaScript with microsecond resolution?

I am aware of timer.js for Chrome, and am hoping there will be a solution for other friendly browsers, like Firefox, Safari, Opera, Epiphany, Konqueror, etc. I'm not interested in supporting any IE, but answers including IE are welcome.

(Given the poor accuracy of millisecond timing in JS, I'm not holding my breath on this one!)

Update: timer.js advertises microsecond resolution, but it simply multiplies the millisecond reading by 1,000. Verified by testing and code inspection. Disappointed. :[

解决方案

As alluded to in Mark Rejhon's answer, there is an API available in modern browsers that exposes sub-millisecond resolution timing data to script: the W3C High Resolution Timer, aka window.performance.now().

now() is better than the traditional Date.getTime() in two important ways:

  1. now() is a double with submillisecond resolution that represents the number of milliseconds since the start of the page's navigation. It returns the number of microseconds in the fractional (e.g. a value of 1000.123 is 1 second and 123 microseconds).

  2. now() is monotonically increasing. This is important as Date.getTime() can possibly jump forward or even backward on subsequent calls. Notably, if the OS's system time is updated (e.g. atomic clock synchronization), Date.getTime() is also updated. now() is guaranteed to always be monotonically increasing, so it is not affected by the OS's system time -- it will always be wall-clock time (assuming your wall clock is not atomic...).

now() can be used in almost every place that new Date.getTime(), + new Date and Date.now() are. The exception is that Date and now() times don't mix, as Date is based on unix-epoch (the number of milliseconds since 1970), while now() is the number of milliseconds since your page navigation started (so it will be much smaller than Date).

now() is supported in Chrome stable, Firefox 15+, and IE10. There are also several polyfills available.

这篇关于JavaScript中的微时序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆