DEV Community

Anurag Vohra
Anurag Vohra

Posted on

Why single-threaded non blocking javascript out performs better than MultiThreaded systems?

Any starter node.js enthusiastic would at least one time ponder, how can a single thread system out perform better than a MultiThreaded system.

And specially many people from multithreaded pardigm, who are evaluating/understading node.js, would rather find it hard to understand : why Node.js(or any single threaded non blocking JS system) is acclaimed better than multi-threaded system (At least for serving web-traffic!)?

Node.JS is not faster (doesnot means its slower either), but rather highly efficient in handling a single thread, as compared to a blocking multi-threaded system handling its single thread!

I have made diagrams to explain this statement with analogies.

multi threaded system work flow

single thread non blocking system

A multi-threaded blocking system is not capable of utilizing its threads power to its full potential, as a thread is blocked until it evaluates each line of code.

Now offcourse one can build a non blockig system on top of a blocking multi-threaded system (thats what Node.js is under the hood), but its very complex. And you have to do it ever where you need non blocking code in your application or program.

Javascript ecosystem (like nodejs) provide this out of the box as its syntax. The JS language sytanx provide all this feature where ever needed. Morever as its part of its syntax, the reader of the code immeditetly knows that where code is blocking and where its non-blocking.

The blocking part of multithreaded-blocking system makes it less efficient. The thread which is blocked cannot be used for anything else, while its waiting for response.

While a non-blocking single threaded system makes best use of its single thread system.

And hence node.js if used properly can out perform many multi-threaded systems.

Discussion (0)