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

发布于 2024-11-07 21:44:19 字数 66 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

所谓喜欢 2024-11-14 21:44:19

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

window.performance.now()

另请参阅:

策马西风 2024-11-14 21:44:19

基于 Jeffery 的答案,要获得绝对时间戳(正如OP想要的那样),代码将是:

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

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

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

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".

森林迷了鹿 2024-11-14 21:44:19

自 UNIX 纪元以来的毫秒数,采用微秒分辨率。

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

(performance.now() + performance.timeOrigin)

规范中的相关引用

此规范定义了一个 API,它提供时间原点和亚毫秒分辨率的当前时间,这样就不会受到系统时钟偏差或调整的影响。

timeOrigin 属性必须返回表示高分辨率时间的 DOMHighResTimeStamp Performance 对象的相关全局对象时间源时间戳

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

时间原点是测量时间的时间值

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

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


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

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

Milliseconds since the UNIX epoch, with the microseconds resolution.

performance.timing.navigationStart has been deprecated! Use the following instead:

(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 timeOrigin attribute MUST return a DOMHighResTimeStamp representing the high resolution time of the time origin timestamp for the relevant global object of the Performance object.

The time origin timestamp is the high resolution time value at which time origin is zero.

The time origin is the time value from which time is measured

The now() method MUST return the current high resolution time.

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


Note that actually it is not that accurate 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 DOMHighResTimeStamp type should be inaccurate enough to prevent attacks: the current minimum recommended resolution is no less than 5 microseconds and, 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.

深空失忆 2024-11-14 21:44:19

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

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

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

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

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

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);
}

初见 2024-11-14 21:44:19

是的!尝试一下出色的 sazze 的 nano-time

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

Yes! Try the excellent sazze's nano-time

let now = require('nano-time');
now(); // '1476742925219947761' (returns as string due to JS limitation)
一念一轮回 2024-11-14 21:44:19

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

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

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

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

兔小萌 2024-11-14 21:44:19

JavaScript 以毫秒为单位记录时间,因此您无法获得如此精确的时间。 smart-aleck 的答案是“乘以 1,000,000”。

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

年少掌心 2024-11-14 21:44:19

我一直在寻找与此类似的东西,这就是我最终想到的,它可能不是 100% 准确,但它是我能得到的最接近的。

var precision = 3;

var timestring = new Date().toLocaleTimeString() + "." + (((性能.now() + 性能.timeOrigin)) / 1000).toFixed(精度).toString().split(".")[1]);

for (i=0;i<1000;i++){
  var precision = 3;
  var timestring = new Date().toLocaleTimeString() + "." + (((performance.now() + performance.timeOrigin)) / 1000).toFixed(precision).toString().split(".")[1];
  var newEl = document.createElement('div');
  
  newEl.innerHTML = timestring;
  document.getElementById("timestamps").appendChild(newEl);
}
<p>Timstamps:</p>
<div id="timestamps"></div>

I was looking for something similar to this, this is what i came up with in the end, it may not be 100% accurate but its the closest i could get.

var precision = 3;

var timestring = new Date().toLocaleTimeString() + "." + (((performance.now() + performance.timeOrigin)) / 1000).toFixed(precision).toString().split(".")[1]);

for (i=0;i<1000;i++){
  var precision = 3;
  var timestring = new Date().toLocaleTimeString() + "." + (((performance.now() + performance.timeOrigin)) / 1000).toFixed(precision).toString().split(".")[1];
  var newEl = document.createElement('div');
  
  newEl.innerHTML = timestring;
  document.getElementById("timestamps").appendChild(newEl);
}
<p>Timstamps:</p>
<div id="timestamps"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文