JavaScript — What is non-blocking I/O

Janith Kasun
IDEA CORE
Published in
4 min readJun 16, 2018

When someone is talking about JavaScript or NodeJS, event loop and non-blocking IO(input/output) are very popular topics, So here is a quick overview of what non-blocking IO really is. I am breaking down this article into a few topics. Please feel free to skip a topic that you may already know.

JavaScript is single-threaded

Well, first of all, JavaScript ‘sucks’? Well actually, Here is why?

The History of JavaScript [INFOGRAPHIC] JavaScript was created by Brendan Eich, a Netscape Communications Corporation programmer, in September 1995. It took Eich only 10 days to develop the scripting language which was then known as Mocha — Wikipedia

“Okay, dude! This was supposed to be a non-blocking IO article. I don’t want to hear another hate speech about Javascript”

Yeah! I get it, but here is the interesting part, Javascript is only developed in 10 days by a single developer. So Javascript didn’t follow the standards of a well-written programming language. Javascript was just written focusing on doing simple programming tasks, maybe changing the color of a DOM element, make a simple calculation. Also, Javascript was not invented to write multi-threaded programming.

Here is what Amazon looked like in 1995 when Javascript was invented,

Figure: Amazon.com in 1995

But as the rise of WWW and internet Javascript became a highly popular programming language in the world. Today we even develop frameworks and rendering animations using Javascript. So to be clear it does not suck anymore. 👊

OS and the state of a process

Here is an interesting video of how the process works on your OS, Feel free to skip this if you don’t feel like watching it. Otherwise, I promise it won’t be a waste of your bandwidth or time.

Anyway, here is the process status diagram,

When you start a new process (maybe word, excel or even a game), It will change its state according to the following diagram,

Figure: The process status Diagram, Source: superuser.com

As you can see, there is new, ready, running, waiting and terminated status. If you struggle with what those are, they are what they sound likes. 😛

But for now, let’s focus on what makes a process to change the state from the ‘running’ state to the ‘waiting’ state. There might be a few factors.

But in order to understand non-blocking IO, what you need to understand is a process might change it state from the ‘running’ state to the ‘waiting’ state, if it needs some kind of input or output to happen in order to continue the execution (maybe get an image from the HDD, receive an HTTP response from a server) which is called an I/O block. Even if the CPU is available for processing, many programming languages do not support to continue the execution until that I/O block is cleared.

So here comes the event loop, Javascript event loop made it possible to avoid these kinds of block and let other independent executions to happen, like render an animation while an HTTP response receives from the server.

Even if Javascript was not able to handle multi-threads in an OS, that gave it the power to manage one thread in the most optimized way like any other programming language couldn’t.

But today Java uses java.nio to address this issue they were having with IO blocking while NodeJS is using child process clustering and microservices to handle multi-treading.

NodeJS and non-blocking IO

NodeJs is fast. That’s the first thing everyone hears when they start learning the language. Non-blocking IO is one of the main reasons why NodeJS is fast. When traditional server-side languages handle 100 requests per second, NodeJS could handle 2000 requests.

There is no perfect language in the world. Every language is perfect in its own way. It will always depend on what you want to develop.

I love Javascript as much as I love Java, Python or PHP. This is my first article and hope you got something out of it.

Please mention your opinion or comments about this article, good, bad or mistakes. I would love to fix them.

Finally, Thank you very much for spending a few minutes to read this. ❤

--

--

Janith Kasun
IDEA CORE

Developer and Gamer who wants to make an impact