Node JS

Node JS is open source and cross-platform JavaScript Runtime to run Javascript outside web browser. Node js is build on Chrome's V8 engine.

Node js is asynchronous event-driven Javascript runtime build for scalable network applications.

JavaScript was meant for web browser only, but using node js, we can use Javascript outside web browser to build applications.


Node JS Applications

Node js is based on Chrome's V8 engine, one of the fastest Javascript engine. Node js use asynchronous event-driven runtime. Node js can handle many concurrent connection at one time.

The JavaScript Execution in Node JS is single thread and Non Blocking making Node JS one of the Fastest Backend Server that can handle many concurrent at time with low RAM and best use of CPU.

Node JS based applications

  1. Web Application Development.
  2. Hybrid Apps Development. (Both Desktop & Mobile)
  3. API Development.
  4. Chat Applications.
  5. Streaming Services for Video and Audio.

Node JS Based Web Servers

  1. Netflix
  2. Linkedin
  3. Uber
  4. Paypal
  5. Nasa
  6. Medium
  7. Slack

Node JS Based Desktop Applications

  1. VS Code
  2. Brackets
  3. Atom
  4. Whatsapp for Desktop

Node JS Performance Secret

JavaScript use Single Thread for execution in a web browser. For long operations like Timers, JavaScript use callback functions. The same way Node JS works. Using this technique, Node JS can handle much more connections than Apache with very low memory.

Callback Example

1

3

2


console.log(1);
    
setTimeout(()=>{ console.log(2)});  // will run after 3
    
console.log(3);
    

As we can see that there is a callback function in setTimeout function, thus JavaScript will not block here and execute third line first. After main thread is free (after printing 3), now callback function will be executed and we will get 2.

The same way database connections are handled in Node JS. By using Non Blocking approach for long waiting tasks, Node JS can handle multiple connections using a single thread.
See example

Blocking Request


const fs = require('fs');
const data = fs.readFileSync('/data.txt');
console.log(data);           
console.log("done");     // will run after data      
    

Non Blocking Request


const fs = require('fs');
const data = fs.readFile('/data.txt',(err,output)=>{
    if(err){
        console.log("error");
    }
    else{
        console.log(output);
    }
});       
console.log("done");      // will run first     
    

It is recommended to use Non Blocking call for file handling in node. Using Blocking call can slow down code performance. So That's how JavaScript being single thread programming language executes very fast just because of callbacks.


Node JS is best for Data-Intensive Applications

The use of single thread and asynchronous event-driven make Node JS best for Data Intensive Applications, like a Web Server where I/O is the biggest issue. Node JS servers best as a web server with very low memory and can server more requests.

Node JS is not best for CPU Intensive Applications where multi-threading is required.


Node Js Vs PHP

Lets Compare Node JS Vs Php as a web server.

Php use Apache Web Server. Apache is Thread & Process Based server where each request is handled by a different thread (process). To assign process to different thread, Apache consumes more memory (RAM).

Node JS is based on Nginx. Nginx is a single thread web server used to handle multiple concurrent request.

Node JS Vs Apache Connections

nodejs vs apache connections
nodejs vs apache connections

Node JS Vs Apache memory

nodejs vs apache memory
nodejs vs apache memory (RAM)