Event Loop Mechanism in Javascript

Enable asynchronous operations in single-threaded language

Abhishek Saini
4 min readAug 11, 2024

JavaScript is a powerful language, particularly for web development, but it has a unique characteristic that can be a bit puzzling — it’s single-threaded. This means that it can only do one thing at a time. So, how does JavaScript manage to handle multiple tasks without freezing up your browser?

Before discussing how Javascript handles multiple tasks, first we need to understand the runtime mechanism of Javascript.

Javascript Runtime Environment

When we run our code on browser, it is processed in javascript runtime environment. Depending on the type of function calls, it has separate execution pipeline.

Following are the components involved in javascript runtime environment:

CallStack

The Call Stack is a fundamental concept in how JavaScript operates. Whenever you call a function in your code, it gets added to the top of the call stack. The JavaScript engine executes the function at the top, and when it’s done, that function is removed from the stack, making room for the next one.

WebApi

Web APIs are powerful features provided by the browser that allow JavaScript to perform tasks that are beyond its core capabilities, like making network requests, handling timers, or interacting with the DOM. These tasks are often asynchronous, meaning they don’t block the main execution thread. When a function that relies on a Web API (such as setTimeout, fetch, or DOM event listeners) is called, the actual work is offloaded to the Web API. Once the task is completed, the Web API sends the result back to the JavaScript environment, where it's placed in the Task Queue and sent back to call stack when its empty.

Task Queue

The Task Queue, also known as the Callback Queue, is where tasks that have completed their asynchronous operations are sent before being pushed onto the Call Stack. These tasks typically include things like handling user interactions (e.g., click events) or processing the result of a setTimeout or setInterval call.

Microtask Queue

The Microtask Queue operates similarly to the Task Queue, but with one key difference, it has higher priority. Microtasks include promises. After each task from the Task Queue is processed, the Event Loop checks the Microtask Queue and processes all microtasks before moving on to the next task from the Task Queue. This ensures that microtasks are handled before any new tasks can be taken from the Task Queue.

NOTE — Callbacks are sent to task queue whereas Promises are sent to microtask queue and microtask queue has high priority over task queue

Now that we know all the components involved in the javascript run time environment, let’s discuss what’s a event loop and how does it fit into this workflow.

What’s a Event Loop?

Event loop is a mechanism in javascript allows to execute code asynchronously without blocking the process thread. It’s the brain in javascript run time environment, which decides on the next task to pick from task queue or microtask queue, if call stack is empty.

Here’s how the Event Loop works:

  1. Check the Call Stack: The Event Loop continuously monitors the Call Stack. As long as there are functions in the Call Stack, the JavaScript engine will keep processing them one by one.
  2. Handle Microtask Queue: Once the Call Stack is empty, the Event Loop checks the microtask Queue to see if there are any pending tasks waiting to be executed. If there are, it will move the first task from the microtask Queue to the Call Stack for execution.
  3. Process Task Queue: If there are no tasks in microtask queue, then it will check for the next task in the Task Queue. If there are any tasks present, they are moved to the Call Stack.
  4. Repeat: This process continues in a loop, hence the name “Event Loop.” This is how JavaScript can handle asynchronous operations, like responding to user input or fetching data from an API, without blocking the main thread.

Why the Event Loop Matters

The Event Loop is essential for maintaining a smooth and responsive user experience in web applications. By allowing asynchronous tasks to be processed efficiently, the Event Loop ensures that JavaScript can handle multiple operations, like animations, data loading, and user interactions, without freezing up or becoming unresponsive.

Solving the Single-Threaded Challenge

JavaScript’s single-threaded nature might initially seem like a limitation, but the Event Loop, along with the Task Queue and Microtask Queue, allows JavaScript to handle complex, non-blocking operations efficiently. By offloading certain tasks to Web APIs and then processing them asynchronously, JavaScript can remain responsive and capable of managing multiple tasks at once.

Conclusion

The Event Loop, along with the Call Stack, Task Queue, and Microtask Queue, form the backbone of JavaScript’s runtime environment. They work together to manage both synchronous and asynchronous tasks, enabling JavaScript to remain efficient and responsive, even when handling multiple operations at once. Understanding how these components interact is key to mastering asynchronous programming in JavaScript and building high-performance web applications.

References

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Event_loop
  2. https://www.youtube.com/watch?v=8aGhZQkoFbQ
  3. https://www.youtube.com/watch?v=eiC58R16hb8

♥️ If you found this Medium story helpful, please share your thoughts in the comments. I would love to hear your feedback. Also, feel free to check out my other articles on Medium.

--

--

Abhishek Saini
Abhishek Saini

Written by Abhishek Saini

Experienced Software Developer • Problem Solver • System Architect • Python • Rest-API • Micro-services • AWS

No responses yet