Search code examples
node.jsevent-loop

Why does NodeJS spawn parallel threads/process to execute an expansive for loop?


I've been testing some code to see how does NodeJS event loop actually works. So I get in touch with this piece of code:

    console.time('Time spending');
    
    let list = [];
    
    for (let index = 0; index < 1000000; index++) {
      const data = JSON.stringify({
        id: Date.now(),
        index,
      });
    
      list.push(data);
    }
    
    console.log(list);
    console.timeEnd('Time spending');

When this code is executed, NodeJS spawns eleven threads on SO (Ubuntu running on WSL 2). But why it does that? This code is not being declared as an async code.enter image description here


Solution

  • That's the worker pool. As mentioned in the great guide Don't block the event loop, Node.js has an "event loop" and a worker pool. Those threads you see are the worker pool, and the size is defined with the environment variable UV_THREADPOOL_SIZE, from libuv, which Node.js uses internally. The reason node.js spawns those threads has nothing to do with your expensive loop, it's just the default behavior at startup.

    There's extensive documentation on how the event loop works on the official Node.js site, but essentially some operations, like filesystem I/O, are synchronous because the underlying operating system does not offer an asynchronous interface (or it's too new/experimental). Node.js works around that by using a thread pool where the event loop submits a task, like reading a file, which is usually a synchronous job, and goes to the next event while a thread does the dirty work of actually reading the file, it can block the thread, but it does not matter, because the event loop is not blocked. When it's done, it reaches back to the event loop with the data. So, for the event loop (and the programmer), the synchronous read was done asynchronously.