The article focuses on the event loop, the order of execution, and how developers can optimize code. The fully detailed schema:
Event loop
Old operational systems didn't support multithreading and their event loop can be approximately described as a simple cycle:
This code utilizes all CPU. It was so in old OS. Modern OS schedulers are utterly complicated. They have prioritization, execution queues, and many other technologies.
We can start describing the event loop as a cycle, which checks whether we have any pending tasks:
To get a task for the execution let's draft
✍️The list of triggers that can put a task into the event loop:
-
<script>
tag -
Postponed tasks:
setTimeout
,setInterval
,requestIdleCallback
-
Event handlers from browser API:
click
,mousedown
,input
,blur,
and etc.-
Some of the events are user-initiated like clicks, tab switching, etc.
-
Some of them are from our code:
XmlHttpRequest
response handler,fetch
promise resolve, and so on
-
-
The promise state change. More about promises in my series
-
Observers like
DOMMutationObserver
,IntersectionObserver
-
RequestAnimationFrame
Almost everything we described above is planned through WebAPI
(or browserAPI).