JavaScript Notes

Day 11

Understanding the Event Loop in JavaScript

JavaScript is a single-threaded language, which means it can only execute one task at a time. However, JavaScript can also handle asynchronous operations, such as fetching data from an API, listening to user events, or setting timeouts. How does JavaScript manage to do multiple things at once without blocking the main thread?

The answer is the event loop. The event loop is a mechanism that allows JavaScript to run asynchronous code in a non-blocking way. In this blog post, we will explore how the event loop works and how it affects the performance and behaviour of our code.

The Call Stack and the Callback Queue

To understand the event loop, we need to first understand two data structures: the call stack and the callback queue.

The call stack is a LIFO (last in, first out) stack that keeps track of the execution context of the current function. Whenever we call a function, it is pushed to the top of the call stack. Whenever a function returns, it is popped from the top of the call stack. The call stack can only hold one function at a time, so if there are multiple functions on the stack, the one on the top is executed first.

💡
You can refer to this blog to know about call stack and execution context

The callback queue is a FIFO (first in, first out) queue that holds the callbacks that are waiting to be executed. A callback is a function that is passed as an argument to another function and is invoked when that function is done. For example, when we use setTimeout, we pass a callback function that will be executed after a certain amount of time. The callback function is not executed immediately but is added to the callback queue.

How the Event Loop Works

The event loop is a loop that constantly checks if the call stack is empty and if there are any callbacks in the callback queue. If the call stack is empty and there are callbacks in the queue, it will take the first callback from the queue and push it to the call stack, where it will be executed. This process repeats until there are no more callbacks in the queue.

The event loop also handles events that are triggered by external sources, such as user interactions, network requests, or timers. These events are handled by event listeners, which are functions that are registered to listen to specific events. When an event occurs, the event listener will create a callback function and add it to the callback queue. The event loop will then process these callbacks as described above.

Look at the below image from MDN to understand the above part of the stack and queue.

A diagram showing how stacks are comprised of frames, heaps are comprised of objects, and queues are comprised of messages.

The event loop allows JavaScript to handle asynchronous code without blocking the main thread. However, this also means that we need to be careful about how we write our code, as some operations can take longer than others and delay the execution of other callbacks. For example, if we have a long-running loop or a heavy computation on the call stack, it will block the event loop from processing other callbacks until it is done. This can lead to poor performance and unresponsive user interfaces.

To avoid blocking the event loop, we should avoid synchronous code that takes too long to execute and use asynchronous code whenever possible. We should also use tools such as promises and async/await to make our asynchronous code more readable and manageable.

Conclusion

The event loop is a key concept in JavaScript that enables us to write asynchronous code in a single-threaded language. By understanding how the event loop works, we can write better code that is performant and responsive.