What the heck is the event loop in Node js?

Saravanan A R
4 min readOct 30, 2018

If you are a beginner to the Node js, you may get stuck with the word “Event Loop”. Don’t worry, at the end of this blog you will get the picture.

Let me break down the things easier to grab.

Fig a

We will examine the event loop with the above code snippet. If you save this file as index.js and run it as node index.js from the terminal. You will get the output Timeout! Run! after 3 seconds. Keep this behavior in mind and continue. In the end, you will get a complete picture.

Whenever you run a js file using the node command, (say running “node index.js” from a terminal) a node instance will be created with a single event loop associated with it. That associated event loop runs in a single thread. That's why node js is called as a single threaded model.

So, now we know that every node js instance has an event loop runs on a single thread.

Now, we will dig deeper into the event loop.

An event loop behavior is similar to the normal while loop. When the loop condition becomes false, node instance returns and terminates its job.

For the illustration purpose, consider the below snippet,

Fig b

When you run the above code, OS will create a process to run it. That process will be alive until the execution complete (ie. while condition becomes false).

Now, come back to the event loop with the above illustration. The event loop is as same as the above mentioned while loop. But event loop won’t run as vigorous as the above code.

Every loop cycle is triggered by the registered events. Every cycle in the event loop is called as “process tick”. Once a loop cycle completes, it will sit and listen for the registered events. If any registered events triggered, another loop cycle will continue. In this new cycle, the callback for those triggered registered events will be executed. This loop cycle will continue until there are no registered events remaining.

Now we will try to make some pseudocode to illustrate the event loop.

In Fig c, task array holds the registered events(don’t care too much about this term as I’ve explained at the end) and its callback(code to execute when the event triggered). while loop(event loop) continues to execute until the task array becomes empty.

  1. For every cycle, the event loop will check if there is any task in the task array which is ready to execute. ( ie. event was triggered for that task)

2. If there is any ready task, event loop will execute its callback.

3. After callback execution, the event is removed from the task array.

4. Event loop will sit and listen for the remaining events.

Fig c. Event loop model illustration

Now, you get how the event loop works. But one thing I have missed to talk about is Registered events. These events can be created by any of the following

  1. setTimeout(), setInterval(), setImmediate()
  2. I/O call like fs.readFile()
  3. http.createServer() etc. etc…

Now go back to the Fig a. How the event loop handle the code in the fig a when we run it?

Fig a
  1. Running the code in Fig a. will create a node instance with a single event loop associated with it.
  2. While execution, setTimeout event and its callback is registered(like task array in our illustration)
  3. Event loop will sit and listen for the setTimeout event.
  4. Once setTimeout event is triggered, event loop starts its next cycle and executes the callback associated with the setTimeout. After callback execution, its event is deregistered. (ie. deleting element from the task array in our illustration)
  5. Now, there are no registered events. (ie. task array is empty and the loop condition becomes false) hence, the event loop cycle completes and node instance returns.

--

--