Understanding the Event Loop
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Explanation:
- When this script runs, "Start" is logged to the console immediately.
- The
setTimeoutfunction is then encountered. Rather than blocking execution, it sets a timer and moves the callback associated with it to the event queue after 1000ms. - "End" is logged next, as it's part of the synchronous code.
- After approximately 1000ms, the event loop detects an empty call stack and dequeues the queued callback to log "Timeout callback."
Call Stack, Message Queue, and Event Table
Call Stack
The call stack is a LIFO (Last In, First Out) data structure. It holds the functions to be executed and tracks the point to which each subroutine should return upon completion.
Message Queue (Event Queue)
The message queue is where callback functions are placed in response to events such as setTimeout triggers, XHR events, and DOM events. When the call stack is empty, the event loop processes events from this queue.
Event Table
This is where long-running tasks are moved out of the main thread. These tasks include timers or callbacks. They notify the event queue when a task is complete.
Microtasks and Macrotasks
JavaScript distinguishes between microtasks (also known as jobs) and macrotasks (or tasks):
- Microtasks: These have a higher priority than macrotasks and include Promises and
MutationObserverobjects. They are queued in the microtask queue and executed immediately after the current operation completes, before any rendering occurs. - Macrotasks: Regular events like
setTimeout,setInterval, andI/Oevents are queued in the macrotask queue.
Example of Microtasks and Macrotasks
Output:
Explanation:
- "Start" and "End" are logged synchronously.
- A promise resolves, placing the associated callback in the microtask queue.
- A
setTimeoutis processed as a macrotask. - "Microtask" is processed before "Macrotask" due to microtasks' higher priority.
Summary
The following table summarizes the key concepts of the event loop:
| Concept | Description |
| Event Loop | Continuously executes the message queue when the call stack is empty. |
| Call Stack | Keeps track of functions to execute in a LIFO order. |
| Message Queue | Stores callbacks for asynchronous events. |
| Microtasks | Higher-priority tasks processed after the current execution ends. |
| Macrotasks | Lower-priority tasks that are processed at each cycle of the event loop. |
| Asynchronous Events | Events like setTimeout, promises, and I/O operations. |
This understanding of the event loop, along with key components like microtasks and macrotasks, is vital for writing efficient and non-blocking code in JavaScript.
Additional Resources
To dive deeper into the event loop, consider exploring:
- The MDN Web Docs for detailed browser implementation guides.
- Node.js documentation for server-side event loop functioning.
- Event loop simulation tools available in developer browsers for hands-on learning.

