当 setTimeout < 时会发生什么?5 节点.js [英] What happens when setTimeout < 5 node.js

查看:43
本文介绍了当 setTimeout < 时会发生什么?5 节点.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找出当 X-> 0 时 setTimeout(fn, X) 的真实行为.

Find out the true behavior of setTimeout(fn, X) when X-> 0.

我正在开发一个使用 setTimeout() 进行 QPS(每秒查询数)测试的工具.最近,当我进行了 1000 QPS 的测试时,我感到惊讶,该测试在本应花费 1 秒的情况下执行了大约 5 秒(不包括任何其他外部因素).

I am developing a tool that makes QPS (Queries Per Second) tests, using setTimeout(). I was recently surprised when I made a test of 1000 QPS that took roughly 5 seconds to execute when it should have taken 1 (not counting any other external factors).

测试适用于较低的 QPS 值,例如低于 100.

The tests works fine for lower values of QPS, bellow 100 for example.

在调查的过程中,我发现一些文章解释说,在进行setTimeout(fn, X) 调用时,当X 趋于0 时,如果X 太小,则会被修剪为更高的值.

While investigating, I found some articles explaining that when making setTimeout(fn, X) calls, when X tends to 0, if X is too small, it will get trimmed to a higher value.

这个例子可以在 2011 年的这个问题中看到,其中旧规范指定如果 setTimeout(fn, X) where X <= 3,X 将自动设置为 4.

An examples of this can be seen in this question from 2011, where the old spec specifies that if setTimeout(fn, X) where X <= 3, X will be automatically set to 4.

这意味着在 setTimeout() 的两次执行之间,我希望等待的最短时间是 4 毫秒.如果我有 setTimeout(fn, 1),它将被转换为 setTimeout(fn, 4).

This means that the minimum amount of time I can ever hope to wait, between two executions of setTimeout() is 4 milliseconds. If I have setTimeout(fn, 1), it will be converted to setTimeout(fn, 4).

我看到的每篇文章都有不同的说法,在我之前发布的问题中,不同的答案也有不同的说法.总体结论似乎是没有结论,因为行为高度不一致".

Every article I see says something different, and in the question I previously posted, different answers say different things as well. The overall all conclusion seems to be "there is no conclusion, as the behavior is highly inconsistent".

回到 node.js,由于我指出的问题已经很老了,我想更新以下内容:

Coming back to node.js, and since the questions I pointed is quite old, I would like an update on the following :

  1. 其中一个答案说 X 的最小值是 1.这仍然准确吗?
  2. 当 X -> 0 时 setTimeout(fn, X) 如何工作?
  1. One of the answers says that the minimum value of X is 1. Is this still accurate?
  2. How does setTimeout(fn, X) when X -> 0 work?

结论

我想要更多关于 setTimeout() 的信息,以便我可以围绕它构建我的代码.将非常感谢找到的文章或答案的文档和日期的链接.

Conclusion

I would like more information regarding setTimeout() so I can build my code around it. Links to documentation and dates of the articles or answers found will be highly appreciated.

感谢您的帮助!

推荐答案

与事件循环有关.想象一堆命令,那个node.js一个接一个执行.

It is related to the event loop. Imagine a pile of order, that node.js execute one after another.

setTimeout 是(简化版)把它放在堆的末尾,在 X 毫秒之前不要执行它".

setTimeout is (dumbed down version) "put this at the end of the pile, and don't execute it before Xmilliseconds".

因此,虽然您确定它会至少等待那个时间,但您仍然需要等待 node.js 将该任务返回到堆的顶部,这可能需要一点时间时间(以毫秒为单位).

So while you are certain it will wait at least that time, you will still need to wait for node.js to get that task back at the top of the pile, which can take a bit of time (in the order of milliseconds).

这也是为什么推荐使用 process.nextTick 的原因,它将任务放在堆的顶部,而不是 setTimeout(callback, 0).

That's also why it is recommended to use process.nextTick, which put the task at the top of the pile, instead of setTimeout(callback, 0).

因此在您的示例中,setTimeout(callback, 1) 不会在内部转换为 setTimeout(callback, 4),只是之前有 3ms 的开销一旦计时器结束,node.js 就会返回该任务.如果事件循环中没有其他内容,并且您的处理器速度很快,您可能可以再减少一毫秒,但 node.js 只是不是为了处理该级别的时间敏感任务而构建的.这将把它置于实时编程的领域,这完全是另一种用途.

So in your example, setTimeout(callback, 1) will not be internally transformed to setTimeout(callback, 4), it's just that there is a 3ms overhead before node.js get back to that task once the timer have elapsed. If there is nothing else in the event loop, and your processor is fast, you may be able to cut down another millisecond, but node.js is just not built to handle time sensitive task at that level. That would put it in the realm of Real Time Programming, which is another use altogether.

从正确的角度来看,setTimeout 在绝大多数用例中用于处理几秒钟,所以大约 1000 毫秒.2~3ms 真的那么不方便吗?

To put things in perspective, setTimeout is, in a crushing majority of use case, used to handle a handful of seconds, so ~1000ms. Is 2~3ms more really that much of a inconvenience?

process.nextTick 还将允许 node.js 清理事件队列,并在您链接大量异步调用时防止 RangeError: Maximum call stack size exceeded 异常.

process.nextTick will also allow node.js to clean the event queue, and prevent RangeError: Maximum call stack size exceeded exceptions when you chain a lot of asynchronous calls.

这篇关于当 setTimeout &lt; 时会发生什么?5 节点.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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