有没有办法使用 JavaScript 以纳秒为单位获取当前时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6002808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:49:58  来源:igfitidea点击:

Is there any way to get current time in nanoseconds using JavaScript?

javascripttimeprecisiontime-precision

提问by Carlos

So, I know I can get current time in milliseconds using JavaScript. But, is it possible to get the current time in nanoseconds instead?

所以,我知道我可以使用 JavaScript 以毫秒为单位获取当前时间。但是,是否有可能以纳秒为单位获取当前时间?

回答by Jeffrey Yasskin

Achieve microsecond accuracy in most browsers using:

使用以下方法在大多数浏览器中实现微秒精度:

window.performance.now()

See also:

也可以看看:

回答by Ronenz

Building on Jeffery's answer, to get an absolute time-stamp (as the OP wanted) the code would be:

基于 Jeffery 的回答,要获得绝对时间戳(如 OP 所希望的那样),代码将是:

var TS = window.performance.timing.navigationStart + window.performance.now();

result is in millisecond units but is a floating-point value reportedly "accurate to one thousandth of a millisecond".

结果以毫秒为单位,但据说是“精确到千分之一毫秒”的浮点值。

回答by Tahsin Turkoz

In Server side environments like Node.js you can use the following function to get time in nanosecond

在像 Node.js 这样的服务器端环境中,您可以使用以下函数以纳秒为单位获取时间

function getNanoSecTime() {
  var hrTime = process.hrtime();
  return hrTime[0] * 1000000000 + hrTime[1];
}

Also get micro seconds in a similar way as well:

也以类似的方式获得微秒:

function getMicSecTime() {
  var hrTime = process.hrtime();
  return hrTime[0] * 1000000 + parseInt(hrTime[1] / 1000);
}

回答by Lightness Races in Orbit

No. There is not a chance you will get nanosecond accuracy at the JavaScript layer.

不。您不可能在 JavaScript 层获得纳秒级精度。

If you're trying to benchmark some very quick operation, put it in a loop that runs it a few thousand times.

如果您尝试对某些非常快速的操作进行基准测试,请将其放入一个循环中,使其运行几千次。

回答by Ryan Lynch

JavaScript records time in milliseconds, so you won't be able to get time to that precision. The smart-aleckanswer is to "multiply by 1,000,000".

JavaScript 以毫秒为单位记录时间,因此您将无法获得那么精确的时间。该智能自作聪明的回答是“乘1,000,000”。

回答by 1111161171159459134

Yes! Try the excellent sazze's nano-time

是的!试试优秀的 sazze 的纳米时间

let now = require('nano-time');
now(); // '1476742925219947761' (returns as string due to JS limitation)

回答by Константин Ван

Milliseconds since the UNIX epoch, with the microseconds resolution.

自 UNIX 时代以来的毫秒数,精度为微秒。

performance.timing.navigationStarthas been deprecated! Use the following instead:

performance.timing.navigationStart弃用!请改用以下内容:

(performance.now() + performance.timeOrigin)


Relevant quotes from the specification

规范中的相关引用

This specification defines an API that provides the time origin, and current time in sub-millisecond resolution, such that it is not subject to system clock skew or adjustments.

The timeOriginattribute MUSTreturn a DOMHighResTimeStamprepresenting the high resolution time of the time origin timestampfor the relevant global objectof the Performanceobject.

The time origin timestampis the high resolution time value at which time originis zero.

The time originis the time value from which time is measured

The now()method MUSTreturn the current high resolution time.

The current high resolution timeis the high resolution time from the time origin to the present time (typically called “now”).

该规范定义了一个 API,该 API 以亚毫秒级分辨率提供时间原点和当前时间,使其不受系统时钟偏差或调整的影响。

timeOrigin属性必须返回DOMHighResTimeStamp的代表高分辨率时间时间原点时间戳有关全局对象的的Performance对象。

时间原点时间戳是其高分辨率时间值时间原点为零。

所述时间原点是从其中时间被测量的时间值

now()方法必须返回当前的高分辨率时间

目前的高分辨率时间是从时间原点到现在的时间(通常称为“现在”)的高分辨率时间。



Note that actually it is not thataccurate for security reasons (to prevent side-channel attacks)

请注意,出于安全原因,实际上它并不那么准确(以防止侧信道攻击)

This specification defines an API that provides sub-millisecond time resolution, which is more accurate than the previously available millisecond resolution exposed by DOMTimeStamp. However, even without this new API an attacker may be able to obtain high-resolution estimates through repeat execution and statistical analysis. To ensure that the new API does not significantly improve the accuracy or speed of such attacks, the minimum resolution of the DOMHighResTimeStamptype should be inaccurate enough to prevent attacks: the current minimum recommended resolution is no less than 5 microsecondsand, where necessary, should be set higher by the User Agent to address privacy and security concerns due to architecture or software constraints, or other considerations.

该规范定义了一个提供亚毫秒级时间分辨率的 API,比之前由DOMTimeStamp. 然而,即使没有这个新的 API,攻击者也可以通过重复执行和统计分析获得高分辨率的估计。为确保新 API 不会显着提高此类攻击的准确性或速度,该DOMHighResTimeStamp类型的最小分辨率应足够不准确以防止攻击:当前推荐的最小分辨率不低于 5 微秒,并且在必要时应由用户代理设置更高,以解决由于架构或软件限制或其他考虑因素引起的隐私和安全问题。