DEV Community

Cover image for Understanding from scratch, The Event Loop in Node JS
Abuka-Victor
Abuka-Victor

Posted on

Understanding from scratch, The Event Loop in Node JS

There are two major components of Node that make Node what it is. These components are the V8 engine and the Libuv library. The V8 engine is basically an interpreter/compiler. It converts the code you write in Javascript to machine code that your computer can understand. The Libuv library is an open source library that facilitates asynchronous I/O operations. It also provides access to the event loop and the thread pool.

The event loop is what is responsible for executing callback functions. In many places it is described as an orchestrator because it receives events/triggers (i.e a promise resolving, a network request, etc) , and runs the callback associated with that event. Now you may ask where does the thread pool come in? Sometimes, the tasks to be carried out are too heavy for the event loop to run on its own, i.e Cryptography operations, compression, so in these instances the event loop offloads these heavy tasks to the thread pool to run. The event loop decides automatically which tasks it deems too heavy so the developer really doesn’t have to bother about that. But of course, you can bother yourself if you really want to, that is fine as well.

The reason the event loop is called such is because it does exactly what it’s name implies. It loops through certain classes of events to determine the priority of callbacks to execute. It first checks, are there any callbacks in the Timer class to run? By timer class I mean, stuff like setTimeout and other similar functions. Next, it goes on to check if there are any I/O callbacks to execute. After that it checks for setImmediate callbacks to run. The setImmediate callbacks are callbacks that you intend to run immediately after an I/O operation is complete. Lastly the event loop checks for close callbacks, i.e closing a server, closing a network connection, etc.

You may want to ask where Promises fit into these classes, well that is actually a special case. The event loop really loves promises so it really doesn’t matter what’s next. As soon as a promise is resolved or rejected, the event loop immediately runs the callback associated with it.

It is important to note that the event loop in all its glory can still be blocked and it is recommended that you do not write code that blocks the event loop for the sake of user experience. But like all recommendations, you can totally ignore this and write terrible code. All the same I will be sharing tips to help guide you in writing non-blocking code for the event loop.

Do not use synchronous versions of functions within callbacks.
Do not do complex computation within callbacks - a loop within a loop within a loop.
Be careful with very large objects. It takes time to stringify or parse large objects to or from JSON.

So that’s it for now from me guys. I really hope you learned something new from this. Happy Coding.

Top comments (0)