简体   繁体   English

node.js的setTimeout的替代方案

[英]Alternatives to setTimeout for node.js

Objective 目的

Discover if there is a more accurate alternative to setTimeout in node.js. 发现node.js中是否有更准确的setTimeout替代方法。

Background 背景

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. 在制作QPS(每秒查询数)测试器应用程序的过程中,我发现使用setTimeout越来越不准确,我需要等待的值越小。

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. 例如,如果我调用setTimeout(fn, 1) ,我将不会等待1ms,而是等待一段随机时间,即尽可能接近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. Node.js不保证回调何时触发的确切时间,也不保证它们的排序。 The callback will be called as close as possible to the time specified. 回调将尽可能接近指定的时间调用。

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

In reality this translates into waiting 2, 3, or even 4ms when in fact I need to only wait 1ms. 实际上,这转化为等待2,3或甚至4ms,而实际上我只需要等待1ms。

In sum, the usage of setTimeout is unpredictable, as is explained in this other question: 总而言之, setTimeout的使用是不可预测的,正如另一个问题所解释的那样:

What did I try? 我尝试了什么?

Since setTimeout is highly inaccurate for small values, I need an alternative - a more accurate one. 由于setTimeout对于小值非​​常不准确,我需要一个替代方案 - 更准确的方法。

To try and find this I searched everywhere for alternatives, but the only thing I found was a library called perf-timers . 为了尝试找到这个,我到处寻找替代品,但我发现的唯一的东西是一个名为perf-timers的库。

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. 这个库声称它比setTimeout更快更轻,但没有办法确认它,因为项目的文档已经从GitHub中删除了,我相信该软件包因为自2012年以来没有添加任何更新或注释而被放弃了。

Questions 问题

  1. Is there any accurate alternative to node.js setTimeout ? 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. 如果您希望在确切的时刻执行,也许您应该在另一个不围绕优化异步操作构建的环境中进行开发。

The closest you'll get to ~ 1ms calls would be setImmediate() and process.nextTick() . 你最接近setImmediate()调用的是setImmediate()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. 但是,这些都不允许您指定延迟,并且您将受到事件循环的支配,但是,您不需要等待下一个循环迭代处理。

You should read the Node.js guide on timers and the event loop . 您应该阅读有关计时器和事件循环Node.js指南 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在其核心中提供的任何内容都不够精确,那么我将使用不同的语言/框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM