Explanation and Detail about NodeJs & epic battle of Java and NodeJs

Madasamy M
10 min readMar 19, 2018

Its really hard to choose which one you should take for an platform for your development for service layer as java or NodeJs, Its almost depends an your requirement and which type of an application your going develop.so just we can compare how they Execute ,process I/O,Performance and,pros and cons of both.

just try to understand Thread Execution in Java and NodeJs.

Execution & Thread in Java and NodeJs:

In basically Java and NodeJs web applications are executes based on threads,first look at java typical application how its get execute and operates thread as well.

Java Execution:

Basically you all aware of that , As .java class file compiled and got byte-code as .class file and java bytecode move through locally or network to runtime environment of java and its get executed in CPU,As you want to know deeply you can google it java compilation process,let me explain further Topics.

How Java Operates Threads:

In java web application As the request has received by the server new thread has picked from thread pool and processing happens on threads and another request there would be a another thread execute as isolation unless there would be a shared resource access.

Threads are continues in process asynchronously as non-blocking, particular thread is wait if there is an blocking in that thread execution and another thread will get CPU allocation to process its own task.

Below is an diagram for java operates threads,

JAVA handling user request & Operates Threads

Above diagram is illustrate about how thread gets execute in java, however simple java program main method treated as parent thread! could you saw it before?

Program:

Class A{}

Output:

java.lang.NoSuchMethodError: main Exception in thread "main"

About output because of When the JVM starts, it creates a thread called “Main”. Your program will run on this thread, unless you create additional threads yourself.

The first thing the “Main” thread does is to look for your static void main(String[] argv) method and invoke it. That is the entry-point to your program.

If you want things to happen “at the same time”, you can create multiple threads, and give each something to execute. They will then continue to do these things concurrently.

next thing is explain about NodeJs and how NodeJs get execute and operate threads?

NodeJS:

NodeJs definition as per Official Documentation

java.lang.Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Often many peoples compare Node with an Programming language like,

I prefer Node to C#. I prefer Node to Java

Node Is Not a Prograaming Language.

Compare Node with an ASP.Net or Django ? is it right ?

I prefer Node to Asp.Net, I prefer Node to django

Do you thing that Node is an framework? No its Not!.

Node Is Not a Framework

In Simple Words NodeJs is an runtime environment for Executing javascript code.

How NodeJs get`s Created?

Before Nodejs Javascript is only Used for Scripting language as run inside our browser,every browser out there as we called javascript engine,It takes javascript code converse into machine code , Below listed example JS Engine used in browsers,

  1. MicrosoftEdge — chakra JS Engine
  2. Mozila — SpiderMonkey JsEngine
  3. Chrome — V8 JS Engine

these variety of JS engine used in different browsers thats why Javascript is behave differently one from another in browser,JS engine provide runtime enviroment for Javascript Code, upto 2009 the only way to execute javascript is an inside browsers,In 2009 Ryan dhal came up with an idea to excecute javascript out of browser, so he took V8 engine which is the best JS Engine Out there and better it, inside V8 Engine C++ program called Node.exe,

In Browser JS Engine,

Document.getElementById(),Window 
---> These Objects are allow us to work with an environment which our code run Provided by (Js Engine in Browsers)

Similar to a browser, Node is an Runtime Environment for Javascript code,In essence Node is an Program that includes V8 Js Engine ,and have some additional modules that gives the capability which is not available in browsers like we can work with FileSystem,Network

In Nodejs,

FS.ReadFile() -->Working with an FileSystem
http.createServer() ---> Listening for Request

above example NodeJs modules.

NodeJs Execution :

We’d like to run JavaScript on backend and get access to low-level API. We also want to run our JavaScript from CLI and REPL. Basically, we want JavaScript to do everything! in Nodejs,Javascript code get compiled in V8 engine developed by google,V8 can be integrated in any C++ project. Just take V8 sources and include them as a simple library. You are now able to use V8 API that allows you to compile and run JavaScript code.

V8 can expose C++ to JavaScript. It’s very important as we want to make low-level API available within JavaScript.

V8 engine compiles javascript to a sequence of machine code instructions, one function at a time (usually, functions are not compiled until the first call).

V8 parses the code and extracts an AST (abstract syntax tree), performs scope analysis in order to understand to which context a symbol refers to, and translates it to machine code instructions.

As you mentioned, V8 is highly focused on performance: besides the full compiler that compiles each function, V8 consists of extra compiler which is responsible for optimizing blocks that identified as frequently used (Known as the Crankshaft)

So no, there’s no interpretation of javascript code, but translation and execution of a machine code, so you need to know more about NodeJs compilation google it, let me explain about NodeJs client Handling requests,Operates Threads.

How NodeJs Operates Threads:

In NodeJs as we received request to server from the client Instead of create a new thread for each user request,the server just add the request to the event queue, each consecutive or concurrent request lined up in queue to be processed by the event loop:

In that diagram flow of an NodeJs Handling user request,

Nodejs handling user request &Operates threads

images source from this site.

Event loop is an Single thread which serves every thread ,event loop thread is run with infinite while loop there is no exit condition, So event loop keep during running entire application runtime ,

event loop pulling thread from queue the order they put in and if the task is just non-blocking and just involved CPU utilisation ,then process it ,if that succeeding task put back into the event queue for further process otherwise return the response to the user,

If there is an blocking task for example File I/O or db operations ,It utilises the one of the thread in thread pool,these child threads to do background tasks, If the blocking task has processing asynchronously and the succeeding the task has put into the event queue and finally the response is send back to the user.

So above just learned about NodeJs and execution of NodeJs and Java.

NodeJs vs Java an epic battle:

NodeJs and Java have their own determined feature, NodeJs was spawned, turning developers’ heads with its speed. Not only was JavaScript faster on the server than anyone had expected, but it was often faster than Java and other options. Its steady diet of small, quick, endless requests for data have since made NodeJs more common, as webpages have grown more dynamic.

On one side are the deep foundations of solid engineering and architecture. On the other side are simplicity and ubiquity.

Where Java Wins?

  1. Java Rock-solid foundation:

we can hear the developers laughing. Some may even be dying of heart failure. Yes, Java has glitches and bugs, but relatively speaking, it’s the Rock of Gibraltar, When you boot up a JVM, you get 20 years of experience from a solid curator determined to dominate the enterprise server compare to NodeJs

2.Java Better IDE`s:

Java developers have Eclipse, NetBeans, or IntelliJ, three top-notch tools that are well-integrated with debuggers, decompilers, and servers. Each has years of development, dedicated users, and solid ecosystems filled with plug-ins.For NodeJs use as text editor Visual Studio code,eclipse.

after NodeJS peaked among developers arriving IDE like IBM’s Node-RED offer intriguing approaches, but they’re still a long way from being as complete as Eclipse.

3.Java Remote debugging

Java boasts incredible tools for monitoring clusters of machines. There are deep hooks into the JVM and elaborate profiling tools to help identify bottlenecks and failures.

The Java enterprise stack runs some of the most sophisticated servers on the planet, and the companies that use those servers have demanded the very best in telemetry.

4.Java huge libraries: There is a huge collection of libraries available in Java, and they offer some of the most serious work around. Text indexing tools like Lucene and computer vision toolkits like OpenCV are two examples of great open source projects that are ready to be the foundation of a serious project.

there are some JavaScript libraries amazing, still java has deep and quality libraries.

5.Java Solid Engineering:

It’s a bit hard to quantify, but many of the complex packages for serious scientific work are written in Java because Java has strong mathematical foundations. Sun spent a long time sweating the details of the utility classes and it shows.

6.Java threads:

Java’s web servers are multi-threaded. Creating multiple threads may take time and memory, but it pays off. If one thread deadlocks, the others continue. If one thread requires longer computation, the other threads aren’t starved for attention (usually).Java multi threading actually useful .

So I have listed some of Java advantages over NodeJs at the time Lets see NodeJs desirable feature.

Where NodeJs Wins?

  1. NodeJs ubiquity:

JavaScript finds a home on the server and in the browser. Code you write for one will more than likely run the same way on both. Nothing is guaranteed in life, but this is as close as it gets in the computer business. It’s much easier to stick with JavaScript for both sides of the client/server divide than it is to write something once in Java and again in JavaScript.

2.NodJs simple build process:

Complicated build tools like Ant and Maven have revolutionized Java programming. But there’s only one issue. You write the specification in XML, a data format that wasn’t designed to support programming logic. Sure, it’s relatively easy to express branching with nested tags, but there’s still something annoying about switching gears from Java to XML merely to build something. With JavaScript, there’s no switching gears.we can directly use as it as in NodeJs.

3.NodeJs with Database Queries:

Queries for some of the newer databases, like CouchDB,mongodb are written in JavaScript. Mixing Node.js and CouchDB requires no gear-shifting, let alone any need to remember syntax differences.

If your using java with Sql or any other databases you need to gear shifting like mapping Java model with your Database table in sql.

4. NodeJs with JSON

When databases spit out answers, Java goes to elaborate lengths to turn the results into Java objects. Developers will argue for hours about POJO mappings, Hibernate, and other tools. Configuring them can take hours or even days. Eventually, the Java code gets Java objects after all of the conversion.

Many web services and databases return data in JSON, a natural part of JavaScript. JSON is now so common and useful that many Java developers use the format, so a number of good JSON parsers are available as Java libraries as well. But JSON is part of the foundation of JavaScript. You don’t need libraries. It’s all there and ready to go,Its an huge advantage for developers.

5. NodeJs Speed:

People love to praise the speed of Node.js. The data comes in and the answers come out like lightning. Node.js doesn’t mess around with setting up separate threads with all of the locking headaches. There’s no overhead to slow down anything. You write simple code and Node.js takes the right step as quickly as possible.

Where both win: Cross-compiling between Java and NodeJs

The debate whether to use Java or NodeJs on your servers can and will go on for years. As opposed to most debates, however, we can have it both ways. Java can be cross-compiled into JavaScript. Google does this frequently with Google Web Toolkit, and some of its most popular websites have Java code running in them — Java that was translated into JavaScript.

There’s a path in the other direction, too. JavaScript engines like Rhino run JavaScript inside your Java application where you can link to it. If you’re really ambitious, you can link in Google’s V8 engine.

All of the code can link to each other harmoniously and you don’t need to choose.

Conclusion:

As we long discussed an mystery about NodeJs and how its executing thread, how its handling user request ,point out with Java,each had its better feasible features but still at developer point of view need conclude our points as some Questions?

Why NodeJs Better than Java?

Ans: NodeJs is an superior when its come to concurrency!

what is concurrency meant?

Ans: NodeJs wins when its come handle thousands of request ,NodeJs Handle more concurrent input and output than java.

Doesn’t java handle it?

Ans: sure it handle but not as many.

As final words ,NodeJS is an Nonblocking I/O ,meant it can do what you need a server do very well ,its receive request and never block itself from receiving more requests while fulfilling its queue.

Which modern companies are using NodeJs:

LinkedIn

Wall-mart

Paypal

Netflix

Uber

The future of web development resides with keeping your server as slim as possible,basically your modern day server delivering JSON data and NodeJs does this well,

So when you are considering a platform, you should definitely consider a lot more than performance when making a decision,Its purely depends upon what kind of applications do you want to build?, Consider the team that’s going to be using it. Walk through the benefits and disadvantages of each platform and then make the decision.

Thanks for Reading!.

--

--

Madasamy M

FullStack Developer at AX2 Technologies pvt Ltd, Working in Web Application Development, Chatbot Development