JavaScript 内部:事件循环运行的时间间隔是多少? [英] JavaScript Internals: At what interval does the event loop run?
问题描述
这是一个关于 JavaScript 内部的问题.
This is a question about JavaScript internals.
假设我有 10 个异步任务,它们都需要 x 秒来运行.在等待响应时,脚本处于空闲状态.
Lets say I have 10 async tasks that all take x number of seconds to run. Whilst waiting for a response the script is idle.
JavaScript 引擎在后台询问任务队列中是否有任何内容".据我了解,这是一个循环.因此,事件循环.我知道在 Node 中这是用 Libuv 实现的.我读过这篇文章,它解释了一点:https://nikhilm.github.io/uvbook/基础.html
In the background the JavaScript engine is asking "Is there anything on the task queue". To my understanding this is a loop. Hence, event loop. I know in Node this is implemented with Libuv. I've read this article which explains a little: https://nikhilm.github.io/uvbook/basics.html
JavaScript 引擎是否会限制此事件循环的运行频率以平衡应用程序的性能?它是否以设定的时间间隔运行?
Do JavaScript engines place any restriction on how often this event loop runs in order to balance out performance of the application? Does it run at a set interval?
如果我有什么不对的地方,请纠正我.我纯粹对这个事件循环运行的时间间隔感兴趣.
If I'm off with anything, please correct me. I was purely interested at what interval this event loop runs.
推荐答案
JavaScript 端本身没有循环.不过在 libuv 中有一个.基本上 libuv 会等到最近的计时器命中或发生 i/o 操作.然后它将在 C 中触发一个回调,该回调调用传递的 C++ 函数 Node,然后触发 JavaScript 代码执行.
There is no loop per se in the JavaScript side. There is one in libuv though. Basically libuv will wait until the closest timer hits or an i/o operation happens. Then it will fire a callback in C, which calls the C++ function Node passed and then this triggers JavaScript code to be executed.
看看这个演示文稿,特别是从幻灯片 33 开始的部分.
Have a look at this presentation, specially the section starting at slide 33.
这篇关于JavaScript 内部:事件循环运行的时间间隔是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!