DEV Community

Cover image for Is Node.js Single-Threaded or Multi-Threaded? and Why?

Is Node.js Single-Threaded or Multi-Threaded? and Why?

Andres Reales on September 07, 2021

Have you been reading multiple articles trying to understand whether Node.js is single-threaded or multi-threaded? Why are there many of them sayin...
Collapse
 
codenameone profile image
Shai Almog

NodeJS isn't more performant than Java. That's a common misconception. It's got better throughput since Javas old IO uses a thread per connection which doesn't scale (but is actually pretty fast). Async Java frameworks includes the benefits of threading and the throughput of NodeJS.

This will be resolved by project Loom which is fast approaching production. Project loom essentially turns threads "partially" green. That would mean you can code synchronously in Java using threads and the simpler stream IO. But the OS won't allocate a thread per Java thread. Only to the optimum amount of threads to get the most out of your CPU.

Collapse
 
jwp profile image
John Peters

Thanks for setting the record straight. The author misspoke on true multithreading.

C# is even better it uses Tasks which always use the least busy CPU. It's not just another thread on a single process, it's an asynchronous call directly to the least busy CPU, which spins up, and queues up Task continuations. Only after the work is done are the results captured using an await. Similar to Javascript promise but many times speedier.

I have heard however that node too can spin up CPU agnostic instances. However this author omitted that discussion.

Node's async first architecture was a brilliant move and covers a lot of territory. However single node instances are nowhere the Speed of Java or C# using Tasks or other well defined threading techniques. The only problem is that browsers only speak Javascript.

Collapse
 
codenameone profile image
Shai Almog

Project loom aims to leapfrog C# by removing the need to denote tasks. It will make the JVM itself smart enough to use just the right amount of native threads seamlessly. It's currently in technology preview stage and hopefully will ship with Java 18 or 19.

Thread Thread
 
jwp profile image
John Peters

Totally cool, I never liked the Task hoops to jump through but use it long enough and it's a second language.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic • Edited

Well I'm not an Java expert but every enterprise app using java like SAP products just feels slow and I instantly feel like I'm back in the 90's. Also he is saying that node is faster in this specific field which it is.

Thread Thread
 
jwp profile image
John Peters

Statement means nothing really as we're not talking about any legacy product. We're talking about what can be done today. For true parallelism today, except on mainframes, the dotnet task construct is a true co-routine design which uses all CPUs in an agnostic way. There's nothing close to it today

Collapse
 
arealesramirez profile image
Andres Reales

If it is an server running an API, there are several programming languages with their respective frameworks that would work. C# could work using .NET Core or Entity Framework just like NodeJS with Express.js

Collapse
 
arealesramirez profile image
Andres Reales

Thanks for sharing! I didn't mean to say NodeJS is more performant than Java. The main reason I was providing the explanation was to give insights with regards to why it is widely used across several projects even though NodeJS architecture runs single-threaded as it provides the performance needed to develop APIs.

Collapse
 
metcoder profile image
Carlos Fuentes

Hi @arealesramirez great article!
I would like to complement with the following:

  • JavaScript is not > Java in terms of the speed. It can in fact be more performant in terms of memory footprint, and even at some point speed, but it requires for the app to run for a time while the hot paths are being identified for optimization by V8 engine. On the other hand, Java already mades optimizations ahead of time because it optimizes during compilation, kinda the same happens with C# (typical thing between an Interpreted and a Compiled language).
  • Node.js doesn't have a single thread, in fact the JS code is executed on a single thread, you're right, but the I/O interaction happens within a thread-pool handled by libuv. This means that the Node.js process itself spawns more than one thread, but your JS code will run on a single thread thanks to V8.
  • About Worker_Threads, you're right, it spawns a V8 vm instance for executing the JS code, but have in mind that is not the same as the Cluster module, they will share the same Event Loop, and for instance the same thread-pool (libuv). Remember that the Event Loop is not within V8 engine, but rather within Libuv. Each browser has it's own Even Loop + VM engine for executing JS code. e.g. Deno uses V8 + Tokio Event engine.

Nice work! Keep it going :)

Collapse
 
coen_c7a57bc84f2f2 profile image
Coen de Wit

Please use the correct facts when referencing other environments like C# (and probably Java too on that topic). With dotnet (C#) you can choose to use AOT compilation when optimizing for startup time. But default it compiles to byte-code which during runtime is JIT compiled in multiple tiers, optimizing during the lifetime. It is even possible to create a profile from test runs to optimise before runtime.

Collapse
 
arealesramirez profile image
Andres Reales

Thanks for complementing.

As per my understanding, worker_threads will share memory but each will have each own independent event loop.

Collapse
 
metcoder profile image
Carlos Fuentes

Kinda, the worker thread can share memory with other threads (made out of the same parent thread) or the parent thread itself. But they just create a new instance of the V8 engine, not another full Node process (where the Node process is who instantiate the event_loop). In Contrast, Cluster in fact instantiates one or more new Node process, for instance they come with it's own event_loop :)

Thread Thread
 
arealesramirez profile image
Andres Reales

Oh yeah! I see what you mean. Using pm2 is a good implementation of clusters where we could have a node per each core of the cpu.
Thanks for the clarifying about the event loop!

Thread Thread
 
metcoder profile image
Carlos Fuentes

Exactly! 😄
No worries, happy to help! As I said, great work ;)

Collapse
 
aristotelesbr profile image
Aristóteles Coutinho • Edited

⚠️ I liked the way you try to clarify the matter, congratulations! I would say that having multiple instances of the V8 unable to communicate and not being fault tolerant only alleviates the problem. 😅

Collapse
 
udlose profile image
Dave Black

.NET can handle more than 7 million requests per second as of their .NET Core 2.2 back in 2019: ageofascent.com/2019/02/04/asp-net.... 8X faster than Node.js (at the time). .NET has seriously improved performance since then (as of .NET 6): https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=plaintext&a=2 (now more than 10X faster than Node.js). Interpreted languages will likely never be able to complete with compiled languages as far as performance goes.

Collapse
 
emreaka profile image
Emre AKA • Edited

Javascript is Just in Time compiled via V8 engine but yes, still .NET is faster. C# gets JIT compiled via .NET runtime by the way.

Collapse
 
philleasfrogg profile image
Philleas Frogg • Edited

Your descriptions of single thread Vs multi thread is nonsense. If you have a sequence of instructions a b c, then they must be executed in that order. You can't go and process c on another thread before b. Did you mean tasks?

Collapse
 
bendev12 profile image
Onayngo Benard

Excellent article Andres.

Collapse
 
arealesramirez profile image
Andres Reales

Thanks @bendev12 !