node.js vs java

Node.js

Node.js is a new technology using javascript to develop back-end services which utilizes google chrome javascript v8 engine. A node.js application is single threaded which means it has single a non-blocking-io thread called event loop. Being single-threaded does not mean node.js cannot handle parallel tasks. Event loop is a part of a library called Lib-Uv written in C/C++ makes it possible to handle multiple tasks in parallel.

u1o2o

Node Process and Concurrency Model

There is a wrong perception about node.js that it runs only one task which not correct. Each task in node.js is put in a task queue called event queue. The eventloop takes each task from the top of event queue and runs the tasks until it reaches an i/o task with a callback.

introduction-to-nodejs-and-isomorphic-javascript-9-638

Once, event-loop hits an i/o task, the task is given to another thread from i/o thread pool. each thread from the thread pool is running in parallel. Once the i/o task is finished, the callback task is being added to the end of the event queue again to be processes by the event-loop.

 

Node.js may use different network addons in addition to its native components one of which is a net library. A node.js application may use express framework to process multiple http requests.

11b7a950-81e3-4654-b1ab-fe87b476d3f0

con.query('SELECT * FROM employees',function(err,rows){
  if(err) throw err;

  console.log('Data received from Db:\n');
  console.log(rows);
});

Node Clustering

Node Clustering essentially means forking new child processes to handle tasks. This forking is basically operating system process forking, with a little plumbing added by Node to allow the parent and child processes to communicate via Inter-Process Communication (IPC). Processes do not share memory.

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    // I changed this line because it affected 
    // syntax highlighting
    console.log('worker', worker.process.pid, 'died');
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);
}

Java:

Java benefits from multi-threading technology by creating a new thread for every request. Each thread might get blocked by an i/o task such as read/right to a database. Since each task is being handled by a separate thread each thread may return the response synchronously after the thread prepares the result.

thread-pools-worker-process-event-cycle

Java thread pool manages the pool of worker threads, it contains a queue that keeps tasks waiting to get executed.

class PrimeThread extends Thread {
  long minPrime;
  PrimeThread(long minPrime) {
    this.minPrime = minPrime;
  }

  public void run() {
    // compute primes larger than minPrime
    //. . .
  }
}

// Somewhere else, in another file 
PrimeThread p = new PrimeThread(143);
p.start();

Performance Comparison:

When it comes to the performance comparison each of the technologies are powerful in different tasks. However, there might be other concerns that affect deciding between java or node.js such as developng work, ease of use, availability of developers, training, make use of technology properly, dev tools, license and organizational management and policies.

In general Node.js avoids CPU context switching between different blocking thread while java CPU keeps doing context switching between different thread. However, if we use cluster to create multiple node.js processes, those processes need to communicate with an IPC mechanism while java uses shared memory between different threads. Shared memory in general is a lot more faster then IPC.

In general Node.js could be up to 30% faster in i/o bind tasks while java is faster and better technology for CPU bind tasks.

Conclusion:

Which one to choose, up to the type of work you want to do, your organization, your talent pool, the group of tools available, management and policies in your organization.

  • Thanks a bunch for sharing this with all folks you really realize what you’re speaking approximately! Bookmarked. Kindly additionally visit my site =). We can have a link exchange agreement between us|