#Java or Node.js

So you have an idea for a great web application and you struggle with choosing the best technology stack.
Do you hesitate between Java based stack and Node.js for your server side?
I’m sure I could help you. The answer is simple:

it depends.

But seriously though.
To decide which one to choose you need to answer to the following question:

What kind of application are you planning to create?

And in my opinion there is one category for which the choice is obvious and it is Java: heavy computational application.

To this category belongs all kind of

    • big data applications
    • data analysis
    • scientific calculations
    • machine learning algorithms applications

The reason why Java would perform better in such cases is Javas multithreading and memory model.

Performance

Node.js and Java memory models differ and this has the most impact for the performance.
Let me explain both models in simplification.

Java model

Java application runs in its own JVM and thus its own process. A process consists of multiple threads.
Threads are like processes with significant differences:

    • threads are more lightweight and relatively easy to create
    • processes have private set of resources but threads share the memory from its process
    • thread can communicate with other threads with ease but processes can communicate with each other by using inter-process communication, which is slower than communication between threads

It means that in Java program job can be divided into multiple threads, which can be executed simultaneously by multiple processors.
This way we can use the full power of the CPUs.

It is very powerful approach, but working with concurrency isn’t easy.

Node.js model

The huge advantage of Node.js is its natural non blocking I/O model. Node.js contain one main thread of execution
and multiple background threads. This background threads perform their tasks in a non-blocking way and are scheduled using a queue
by a main thread.
In this approach all changes are performed in main thread, which means that it is easier to work with, but for the performance cost.

Performance summary

Is that it? Is the only difference that Java can be executed in parallel and Node has non blocking I/O operations?
Not exactly.
Java also have support for non-blocking I/O operations, but it’s not a first class citizen as it is in Node.
Node on the other hand also can be used with parallelism. To do so you need to use Cluster library.
This library creates child processes and communicate via IPC (which is slower than communication in Java threads).

As you can see both platforms can handle similar tasks but in favor of Java in case of performance.
Let’s review other aspects which could have an influence on which platform to choose.

Security

Over two decades of improvements Java has has become rock-solid language for secure enterprise apps.
I would consider its strong static typing as a great advantage, especially for a large application.
Thanks to static type we receive error about wrong types during the compilation.
All refactoring could be done with greater confidence. Types serve as a self explaining documentation.

As for Node.js, you can use it with JavaScript or if you want to benefit from static typing you can use TypeScript.

Team performance

By team performance I mean how fast and easy application can be created and maintained.

Verbosity

There is no doubt Java is far more verbose than JavaScript or TypeScript. In Java you have to create classes,
use proper type for any variable user creates, specify getters and setters…

Tooling

Writing code for Node usually means to write code in developers favourite editor without any fancy support.

In Java world you have at least 3 great IDE to choose: IntelliJ, Eclipse, Netbeans. Each one of these
contains a great amount of functionality, which makes a life of a developer a lot easier, eg:

  • language support
  • smart completion
  • inspections and quick-fixes
  • code navigation
  • class hierarchy view
  • refactoring shortcuts
  • debugger integration with view
  • test runners
  • decompilers
  • build-in tools (version control, build tools)

Mastery an IDE highly improves productivity and joy of work.

Isomorphism

This point is dedicated to JavaScript.
One of the advantage of using Node both on client and server side can share common code and libraries.
Situation where part of the business logic has to be moved from client to server (or in the opposite direction)
is much easier to handle when code is written in the same language.

Also in Node you don’t have problems with JSON which is widely used in services communication and in a way data are stored in some
NoSQL databases like mongoDB.

Summary

There is no Holy Grail of a programming stack. Each stack should be chosen according to requirements.
In base case scenario a rule of thumb might be to pick Java for larger projects and with heavy processing.
There is a chance for a compromise. In an era of micro services, some jobs could be delegated to services written in Java
and some delegated to services written in Node.js.

For each problem proper approach should be applied.
Don’t follow the buzzwords. Rely on facts and experts opinion.
If you have an idea for an app and still hesitate on a backend stack – write to us. We will help you to choose
optimal tools to complete resilient apps.
If you can’t decide on a frontend side stack you can read this great article,  by Mateusz C.,
where he is comparing 2 major frontend frameworks Angular and React.
Well… actually, one of them is not a framework. But to find out which one and why, you need to read it.

Leave a Reply

Your email address will not be published. Required fields are marked *