node.js 的 setTimeout 的替代方案 [英] Alternatives to setTimeout for node.js

查看:57
本文介绍了node.js 的 setTimeout 的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现是否有更准确的替代setTimeout 在 node.js 中.

Discover if there is a more accurate alternative to setTimeout in node.js.

在制作 QPS(每秒查询数)测试应用程序的过程中,我发现使用 setTimeout 越不准确,我需要等待的值越小.

In the process if making a QPS (Queries Per Second) tester app, I discovered that using setTimeout is increasingly inaccurate the smaller the value I need to wait is.

例如,如果我调用 setTimeout(fn, 1),我将不会等待恰好 1 毫秒,而是会等待随机时间,即尽可能接近 1 毫秒.Node.js 文档清楚地说明了这一点:

For example, if I call setTimeout(fn, 1), I will not wait exactly 1ms, instead I will wait a random amount of time, that is as close as possible to 1ms. Node.js documentation makes this clear:

回调可能不会被精确延迟地调用毫秒.Node.js 不保证确切的时间回调何时会触发,也不会触发它们的顺序.回调将是尽可能接近指定的时间调用.

The callback will likely not be invoked in precisely delay milliseconds. Node.js makes no guarantees about the exact timing of when callbacks will fire, nor of their ordering. The callback will be called as close as possible to the time specified.

来自 https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_arg

实际上这意味着等待 2、3 甚至 4 毫秒,而实际上我只需要等待 1 毫秒.

In reality this translates into waiting 2, 3, or even 4ms when in fact I need to only wait 1ms.

总而言之,setTimeout 的用法是不可预测的,正如在另一个问题中所解释的:

In sum, the usage of setTimeout is unpredictable, as is explained in this other question:

由于 setTimeout 对于小值非​​常不准确,我需要一个替代方案 - 一个更准确的.

Since setTimeout is highly inaccurate for small values, I need an alternative - a more accurate one.

为了尝试找到这个,我到处寻找替代方案,但我找到的唯一的东西是一个名为 perf-timers.

To try and find this I searched everywhere for alternatives, but the only thing I found was a library called perf-timers.

这个库声称它比 setTimeout 更快更轻,但没有办法确认,因为项目的文档已经从 GitHub 中删除,我相信这个包被放弃是因为自 2012 年以来未添加任何更新或注释.

This library claims that it is faster and lighter than setTimeout, but there is no way to confirm it since the documentation from the project has been removed from GitHub and I believe that the package was abandoned because no updates or notes were added to it since 2012.

  1. node.js 有没有准确的替代setTimeout?

推荐答案

这主要是受事件循环、其回调队列和您的处理能力支配的结果.如果您希望在准确的时刻执行,也许您应该考虑在不是围绕优化异步操作构建的替代环境中进行开发.

This is mostly a consequence of being at the mercy of the event loop, its callback queue, and your processing power. If you're looking to execute at an exact moment, perhaps you should look to develop in an alternative environment that isn't built around optimizing asynchronous operations.

最接近约 1 毫秒调用的是 setImmediate()process.nextTick().但是,这两种方法都不允许您指定延迟,并且您仍然会受到事件循环的支配,但是您无需等待下一次循环迭代来处理.

The closest you'll get to ~ 1ms calls would be setImmediate() and process.nextTick(). However, neither of these allow you to specify a delay and you'll stil be at the mercy of the event loop but, you won't need to wait for the next loop iteration to process.

您应该阅读 Node.js 计时器指南和事件循环.如果阅读后您认为 Node 在其核心中提供的任何内容都不够精确,那么我会考虑使用不同的语言/框架.

You should read the Node.js guide on timers and the event loop. If after reading that you don't think anything Node is offering in its core is precise enough, than I would look into using a different Language/Framework.

这篇关于node.js 的 setTimeout 的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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