Nodejs event loop in a nutshell

Lakshan Bandara
3 min readDec 28, 2020

Here we are going to look how nodejs event loop works compared to real world scenario.

Before version 10, nodejs used to be a single threaded programming language. In version 10 it introduces worker thread module where developers can use the true power of multicore CPU s. Node version 11 and onwards worker threads module became a standard module with nodejs.

Is that mean before version 10 , was node a completely single threaded and while performing one task all the other task got blocked?

Answer is “Yes” nodejs was a single threaded , and “No”: nodejs didn’t block all the other tasks. To achieve this behaviour nodejs introduced concept called “Event loop”. Event loop is a queue of events which executes one after other .

How Event loop handles synchronous events

To understand this, let’s think about a restaurant where only one waiter serves all the tables. Let’s take 2 tables as 1 and 2. First our waiter will go to the table 1 and take their order and then go to the table 2 and take their order.

Think about a scenario where waiter takes the order from table 1 and in the middle of the order table 2 customer ask to take his order as well.

Should our waiter stop the noting order 1 and go to serve the table 2 ? No right?. The usual behaviour is when the person in table 2 raise his hand, our waiter will create note in mind where he needs to serve next. Likewise he will create a queue in his mind.

This same theory has used in the nodejs “Event loop” as well.

Let’s look at the coding example.

const result = (999 * 199 * 100)99 * 1002   // assume this takes 10s 
console.log(result)
console.log("finished")

Output :

12344      // assume this the resultfinished

Program waits 10s until the execution completes and then logs the “ finished ”

How Event loop handles synchronous events

Let’s take the same restaurant scenario as the above and add a new character to that. Now there is a chef who prepare the orders takes by the waiter.

Our waiter takes the order from table 1 and give that order to the chef. Should he wait in the kitchen until the food is prepared ? No, He can go to other tables and take their orders until the food is ready.

While our waiter takes the order from table 2 , a customer from another table raise their hand to get attention of the waiter. And after few seconds kitchen notifies the table 1‘s food is ready. Is our waiter going to stop all the order taking task to collect the meal from the kitchen?

No, he takes a mental note and add that to the queue in his mind respectively. After taking the order from table 2 he reaches the customer who raised his hand and then go to the kitchen to collect the table 1’s meal.

In this scenario , call from the kitchen is equal to a callback from an asynchronous event.

Let’s apply that to nodejs coding example:

setTimeout(()=> {
console.log('callback')
}, 0)
console.log("finished")

Output :

finished
callback

What happen here is, first program execute the setTimeout , while setTimeout is executing finished console log events try get programs attention. Then program add that into event queue. After executing the setTimeout, program also add that callback to the event queue.

callback > finished > setTime

Whenever the thread pool get a vacant thread, event by event from the queue get assigned to the thread and get executed.

--

--