每个事件循环处理多少 JS 语句?
How many JS statements get processed per event loop?
在检查事件 queue/per tick/per 循环之前是否有一定数量的 指令 语句被处理(说同样的话,我觉得?)
在检查事件队列之前没有设置要处理的指令数。每条消息都是 运行 完成。来自 Mozilla 文档 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop):
Each message is processed completely before any other message is processed. This offers some nice properties when reasoning about your program, including the fact that whenever a function runs, it cannot be pre-empted and will run entirely before any other code runs (and can modify data the function manipulates). This differs from C, for instance, where if a function runs in a thread, it may be stopped at any point by the runtime system to run some other code in another thread.
A downside of this model is that if a message takes too long to complete, the web application is unable to process user interactions like click or scroll. The browser mitigates this with the "a script is taking too long to run" dialog. A good practice to follow is to make message processing short and if possible cut down one message into several messages.
Is there a set number of instructions that get processed before checking the event queue/per tick/per loop (ways of saying the same thing, I think?)
没有,没有。
在 node.js 架构中,当从事件队列中拉出事件时,它会绑定到回调。解释器调用那个回调,然后那个回调 运行s 完成。只有当它 returns 并且堆栈再次为空时,它才会检查事件队列中是否还有另一个事件到 运行。
所以,跟指令数完全没有关系。 node.js 运行s 你的 Javascript 是单线程的,所以 Javascript 之间没有时间切片,这听起来像是你的问题可能是预期的。一旦调用了与事件队列中的 even 相对应的回调,该回调 运行s 直到它完成并且 returns 控制权返回给解释器。
所以,它是这样的:
- 从事件队列中拉取事件
- 调用与该事件关联的 Javascript 回调
- Javascript 回调 运行 秒直到完成,然后 returns 来自回调
- node.js 内部检查下一个事件的事件队列。如果存在事件,则转到步骤 1 并重复
- 如果没有事件存在,则进入休眠状态,直到有事件放入事件队列。
实际上,这有点简化,因为有几种不同类型的事件队列,它们具有优先顺序,其中一个先行,但这描述了与您的问题相关的一般过程。
在检查事件 queue/per tick/per 循环之前是否有一定数量的 指令 语句被处理(说同样的话,我觉得?)
在检查事件队列之前没有设置要处理的指令数。每条消息都是 运行 完成。来自 Mozilla 文档 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop):
Each message is processed completely before any other message is processed. This offers some nice properties when reasoning about your program, including the fact that whenever a function runs, it cannot be pre-empted and will run entirely before any other code runs (and can modify data the function manipulates). This differs from C, for instance, where if a function runs in a thread, it may be stopped at any point by the runtime system to run some other code in another thread.
A downside of this model is that if a message takes too long to complete, the web application is unable to process user interactions like click or scroll. The browser mitigates this with the "a script is taking too long to run" dialog. A good practice to follow is to make message processing short and if possible cut down one message into several messages.
Is there a set number of instructions that get processed before checking the event queue/per tick/per loop (ways of saying the same thing, I think?)
没有,没有。
在 node.js 架构中,当从事件队列中拉出事件时,它会绑定到回调。解释器调用那个回调,然后那个回调 运行s 完成。只有当它 returns 并且堆栈再次为空时,它才会检查事件队列中是否还有另一个事件到 运行。
所以,跟指令数完全没有关系。 node.js 运行s 你的 Javascript 是单线程的,所以 Javascript 之间没有时间切片,这听起来像是你的问题可能是预期的。一旦调用了与事件队列中的 even 相对应的回调,该回调 运行s 直到它完成并且 returns 控制权返回给解释器。
所以,它是这样的:
- 从事件队列中拉取事件
- 调用与该事件关联的 Javascript 回调
- Javascript 回调 运行 秒直到完成,然后 returns 来自回调
- node.js 内部检查下一个事件的事件队列。如果存在事件,则转到步骤 1 并重复
- 如果没有事件存在,则进入休眠状态,直到有事件放入事件队列。
实际上,这有点简化,因为有几种不同类型的事件队列,它们具有优先顺序,其中一个先行,但这描述了与您的问题相关的一般过程。