JavaScript - What is non blocking I/O
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 about what non-blocking IO really is. I am breaking down this article into few topics. Please feel free to skip a topic that you may already know. So let’s get to the topic.
JavaScript is single threaded
Well first of all, JavaScript sucks! Well actually, Here is why?
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. 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 task, like maybe changing a color of a DOM element, make a simple calculation. And also, Javascript was not invented to write multi threaded programming.
Here is what Amazon looked like in 1995 when Javascript was invented,
But as the rise of WWW and internet Javascript became a most popular programming language in the world. Today we even create frameworks and rendering animations using Javascript.
OS and the state of a process
Here is an interesting video of how 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.
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,
As you can see, there is new, ready, running, waiting and terminated status. If you struggle 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 running state to waiting state. Their might be few things.
But in order to understand non-blocking IO, what you need to understand is a process might change it’s state from running sate to waiting state, if it needs some kind of input or output to happen in-order to continue the execution (maybe get a image from the hdd, get a http request from a server). That’s called an I/O block. Even if the CPU is available for processing, many programming languages does not support to continue the execution util that I/O block is cleared.
So here comes the event loop, Javascript event loop made it possible to avoid these kind of block and let other independent executions to happen, like render an animation while a http response receive from the server.
Even if Javascript was not able to handle multi threads in a OS, that gave it 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 IO blocking issue, while NodeJS is using child process clustering and micro services to handle multi-treading.
NodeJS and non blocking IO
NodeJs is fast. That’s the first thing everyone hear when they start learning the language. Non blocking IO is one of the main reasons why NodeJS is fast. When traditional server side languages handles 100 requests per second, NodeJS could handle 2000 requests.
There is no perfect language in the world. Every language is perfect on 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. Hope you got something out of this article.
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 few minutes to read this. ?
BTW, this article was already published on Medium. I'm just exploring new options.