This article provides a comparison between Node.JS and other scripting languages, especially the most popular scripting languages of the current decade. I will provide some valid arguments, examples and facts in this article that will surely make you feel- "Yes, now it's the right time to switch to Node.JS".
I'll also try to solve several puzzles regarding Node.JS, some of these paradigms are as follows:
- Why Node.JS?
- How is it better than PHP?
- Node.JS vs AJAX | Asynchronous nature
- Node.JS vs other scripting languages.
- Node.JS vs Python | Performance
- Node.JS vs Ruby | Flexibility
And many more others.
Node.JS | False Assumptions
It is a very popular opinion these days:
"Event-driven programming can be overwhelming for beginners, that can make Node.JS difficult to start with."
But this is only a false assumption, in this article I'll give you a sound feel of Node.JS and make you think again and again, "It really is the right time to switch!". So get ready for this Node.JS journey, because I'll explain why Node.JS is growing and gaining popularity among developers day by day.
Node.JS | Description
Before getting started I want you to go and read my first article based on its importance along with cons and pros.
Go, have a look at this link in order to understand the basics and the following points regarding Node.JS :
Basics of Node.js Based on Annual Conference Session
Now here are some points you first need to understand about Node.JS, so that you can have a mind set, how it's different from other traditional server-side scripting languages:
- Node.js is nothing but server-side JavaScript.
- Single threaded environment.
- Asynchronous event driven server-side JavaScript.
- Non blocking I/O and File API.
- Runs on Google JS ver-8 Engine.
- Nodes apps are created.
- Achieving Scale and Concurrency.
- A file in node.js is called a Module.
- High performance.
- Written in C and C++
- Highly scalable .
Asynchronous Programming | Node.JS
- Asynchronous event driven server-side JS
- Uses a Module Architecture
- Simplifies Complex Applications
If your are already familiar with the concept of "Asynchronous programming" then clap for yourself because now it's going to be as simple as a cup of tea for you but in case you are not then don't worry because I do like to write generally on blank papers, and am sure everyone will like this.
We can explain Asynchronous programming as:
"A single thread event system like Node.JS works by placing events in a queue and processing them one by one calling the appropriate event handler."
This concept is most important to remember about Node.JS.
Node.JS vs PHP
Node.JS is only an environment where you need to do everything yourself. It may sound crazy to you. The thing is there is no default HTTP server or any other server for the several required purposes.
In the node.js environment one script handles all the communication with the respective clients. This considerably reduces the number of resources used by an application.
This fact can be overwhelming for a beginner.
Here I am comparing both with an example:
Node.JS | Code Snippet
var i, l, m, n, maxval;
maxval = 10000000;
var v = Date.now();
for(i = 0; i<maxval; i++)
{
l = 365 + 1024 + i;
m = 365 * 1024 + i;
n = 365 / 5 + i;
}
console.log(Date.now() - v);
PHP | Code Snippet
$l = null;
$m = null;
$n = null;
$i = null;
$maxval = 10000000;
$v = microtime(true);
for(i = 0; i<maxval; i++)
{
$l = 365 + 1024 + $i;
$m = 365 * 1024 + $i;
$n = 365 / 5 + $i;
}
var_dump(microtime(true) - $v);
Explanation
I did execute the preceding program using CMD so that I can see the delay in code snippet execution. I performed this execution 10 times and averaged the result, and after execution I was shocked due to the performance results.
Now have a look at this table, and get ready for dropping your jaw. In this table I mentioned the performance measure of both PHP and Node.JS. (The time is in milliseconds.)
Here is the table containing the performance measurements between these two:
In the very first outcome the PHP code snippet executes much faster than Node.Js but after several sets of iteration Node.JS wins this race because it's speed of execution.
Result
So, on the basis of the facts above and example now we can easily say that:
“PHP is around 92-93% slower than Node.JS”