About the specific implementation of the browser event loop?

there are many articles on browser event loops, including main threads, task queues, micro tasks, and macro tasks. It"s more or less the same! There is one thing I still don"t understand: how exactly is the browser implemented?

first! If an event occurs (whether it"s a network or mouse event.) this event needs to be added to the task queue to be processed, so the question is, how did he add the event to the queue? Or how did the browser know that something had happened?
my guess on this point is that maybe all asynchronous operations will call a function when there is a result (whether it is a function or a ghost, it is the same code anyway). The function of this code is to finally add the event handler to the task queue through various calculations.

then! How does the browser execute the loop mechanism? I have two guesses:
1, maybe the behavior of checking the queue in a loop is triggered by events. For example, when an asynchronous event arrives, the browser will not only call the code I mentioned above to place the event handler, but also trigger a queue check immediately after placement. If there is an event task, it will be taken out and executed immediately.
2, this is simple and violent. Maybe the browser will open another thread. There is a timer buried in this thread, just like setInterval, to check the task queue every 1 millisecond or even 0.5 millisecond. But this seems to be a waste of resources!

what do the bosses think?


single thread just says that the main thread is single thread, not that there are no other threads, so as far as I understand it, event loop probably means to open an event loop thread to execute while (true) .


graphic window programs generally use loops to capture and handle events.


is similar to what you think of 2, but actually opens another thread to do event loop.


js abstraction is single-threaded (at least the standard implementation is single-threaded)


my understanding is that after the browser has finished processing one task, it starts to poll for the next task (there should be some strategies in it) before moving on to the next task.

Menu