The Internals of Node.js

Part 4. “Is Node Single-threaded?”

Song Cho
4 min readJul 13, 2019

You may have heard that node.js is a single thread, and so you may be surprised to know the fact that is not entirely true. I want to clarify that the event loop that we have just discussed in the previous section is truly single-threaded.

When we start up a program with Node.js, a single instance of the event loop is created and placed into one thread. This can be understood that our program can only run on one core which doesn’t look like we are efficiently using our resources since we have many cores inside of our CPU on our computer.

Simply speaking, our program might not run as fast as it could be because it’s limited to one single thread. However, the real truth is that some of the functions that are included inside of the standard library (or framework) of Node.js are not actually single-threaded!

Some of the functions inside of Node.js actually run outside of our event loop and outside of that single thread.

“The event loop uses a single thread but a lot of the code that we write does not actually execute inside that thread entirely.”

“Testing whether Node.js is Single-threaded”

We are going to answer this question through witing codes and then test it.

Let’s first start off by requiring in crypto which is a module that is included in Node.js. Then, we are going to execute ‘pbkdf2’ function from the Crypto library and check how long it takes to run that function. (It approximately takes one second.)

We don’t really need to know what ‘pbkdf2’ function does, but instead, all we need to understand is that it takes some amount of time to complete the task.

If you look at the codes carefully, we are recording the start time when we execute our function and then check the time when the callback is triggered. We could get out how long it took to print out time in milliseconds by subtracting the start time from the new current time.

Instead of testing this process one single time, we will do one more to see more interesting results.

Now before we actually run these codes, I want you to think about the result for seconds. If we think the event loop is a single-threaded, a thread will present a linear series of instructions to our CPU. Normally, one call to ‘pbkdf2’ function takes one second.

So with two function calls, and if this really was a single-threaded system, I would have expected this entire process to take two seconds total.

Now, let’s run our code and see what actually happened.

We have expected the first function to complete and the second function would execute if Node.js was single-threaded. However, as you can see, both logs have been printed out at almost the same time. Clearly, something is happening outside of a single thread.

What happened? We’re going to deep dive into why this is happening in the next section.

Recap

  • Some of the functions that are included inside of Node.js are not actually single-threaded.
  • The event loop uses a single thread but a lot of the code that we write does not actually execute inside that thread entirely.
  • Node.js is a single-threaded language that in the background it uses multiple threads to execute asynchronous code.

Reference

--

--

Song Cho
Song Cho

Written by Song Cho

Software Engineer@ Humanscape

No responses yet