JavaScript Internals: At what interval does the event loop run?

跟風遠走 提交于 2019-12-11 13:26:40

问题


This is a question about JavaScript internals.

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.

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

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.


回答1:


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.

Have a look at this presentation, specially the section starting at slide 33.



来源:https://stackoverflow.com/questions/39109205/javascript-internals-at-what-interval-does-the-event-loop-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!