问题
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