The Internals of Node.js

Part 3. Event loop

Song Cho
4 min readJun 29, 2019

“Implementation of Event Loop”

The event loop is used by Node.js to handle asynchronous code

Understanding how the event loop works is extremely important because a lot of performance depends on how the event loop behaves. Whenever we start up a Node.js program, it automatically creates a single thread and then executes all of our code.

Event loop act as a control structure that decides what a single thread should do at any given point in time.

We’re going to write some fake code to better understand what the event loop does inside of our program.

Let’s assume “Event loop” as a “while loop”, and so the body of while loop is going to execute again and again. We refer to this execution of the body as a tick.

Inside the while condition, we are going to run the “shouldContinue” function which decides whether or not our event loop should continue running for another tick, and it checks three separate conditions.

1. First it looks if there are functions such as setTimeout, setInterval or setImmediate and still needs to be executed.

2. Second check is to look if there are any pending operating system tasks. An example of an operating system task is an HTTP server listening to requests on some port.

3. Finally, it checks to see if there are any long running operations that are still being executed inside of our program. An example of a long-running operation would be like a file system(FS) module to read some files off the hard drive.

All three of these checks may consist of array type and holds a list of tasks that keeps track of any of these pending tasks and tasks that are currently being executed.

So when we run myfile.js, these arrays are going to be created and then all the tasks for running the program will be added such as running setTimeout, HTTP server, or FS module.

“Internals of event loop”

We’re now going to move to the body of our event loop. There are about 5 steps that we believe it would most likely to happen inside the event loop.

1. Node.js looks at the array of “pendingTimers” and it looks at all the different functions that have been passed to setTimeout and setInterval.

2. looks at “pendingOSTasks” and “pendingOperations” and calls relevant callbacks.

It basically looking at that collection of all the different tasks and operations and if any of these things have been completed or any new events have been triggered the Node.js executes the relevant callbacks.

3. Third step pauses execution temporarily.

During this pause execution, Node.js just waits for new events to occur and will continue whenever some number of events occur. And that is whenever a new pending OS task is done, whenever a new pending operation is done or whenever a timer is about to complete.

4. looks at “pendingTimers” and call any setImmediate.

But in this case, Node.js doesn’t care about setTimeout in setInterval function calls. It only looks for functions that have been registered with setImmediate and remember setImmediate is very similar to setTimeout and setInterval.

5. Finally, handles any ‘close’ events.

It is just about handling cleanup code and making sure that we don’t have any remaining tasks inside of our program.

OK! Let’s recap what we’ve discussed and we’re going to move on to the next topic, “Is Node single thread?.

Recap

  • Event loop act as a control structure that decides what a single thread should do at any given point in time.
  • Inside the event loop, it holds a list of tasks that keeps track of any pending tasks and tasks that are currently being executed.
  • There are about 5 steps that we believe it would most likely to happen inside the event loop.

Reference

Sign up to discover human stories that deepen your understanding of the world.

--

--

Song Cho
Song Cho

No responses yet

Write a response