DEV Community

Anurag Vohra
Anurag Vohra

Posted on

2

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.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay