Back to Home

Node.js or Java: performance, resources, flow control, popularity and personal experience

node.js · java · javascript · web development · performance comparison · personal experience

Node.js or Java: performance, resources, flow control, popularity and personal experience

My colleagues and I recently discussed the popularity of some technologies - in particular, Java and node.js. After a short Internet surfing, it turned out that it was these technologies that many information giants use to develop and maintain their sites on the network. Below, I will give only a small part.

Companies using Java:

image

Companies using node.js:

image

It is also less interesting that according to a search on indeed.com ( 06/28/2019 ) at the request of Java Developer (30272 vacancies) and node.js developer (7401 vacancies), specialists in These technologies are quite in demand.

image

But all this is just general information regarding popularity. The information that prompted me to delve deeper into the topic and speculate on the subject of technical features, which led to the writing of this article.

Why are they worth comparing?


Java is a language, node.js can be called an ecosystem built on the basis of JS, and, first of all, based on V8 - an engine from Google.

However, when we talk about Java, we are talking not only about the language, but about the Java virtual machine, as well as the entire ecosystem and infrastructure built around this machine. At a minimum, they can be compared on this basis - as a result, in both cases, we have a runtime . In the case of Java, it is a virtual machine. In the case of node.js, it is a V8 engine that is available on most operating systems, such as Windows, Linux, MacOS, and less well-known ones.

Developers can write code using the same language, and this will work in more or less the same way on different operating systems due to the fact that there is a runtime. The runtime environment affects how the interaction with the OS occurs. In addition, they can be compared since they are used to solve a similar range of problems .

V8 and JVM


When the JS code gets into v8, just in time compilation into byte code that is used in the virtual machine is performed, the JS code runs faster and faster.
Byte code is a high-level intermediate language, so in a Java virtual machine they write not only in Java, but also in Scala and Kotlin.

There are prerequisites that in the near future for V8 it will be possible to use not only JS but also TypeScript or others. At the moment, there is a transiling of these languages ​​in JS. In the future, they will probably be supported out of the box, and everything will work much faster.

Now there is a continuous development of V8, and by and large, the appearance of new versions of node.js is associated with the advent of a new version of the V8 engine. They are directly interconnected.

Node.js: advantages and disadvantages


Node.js was created by Ryan Dahl in 2009.

Node.js itself includes several main components:

  • V8 engine
  • libuv library, which is responsible for the central part of the node - an event loop, which interacts with the OS, as well as for asynchronous input / output (I / O);
  • from a set of various JS libraries and directly the JS language itself.

Let's move on to its pros and cons.

Pros:

  • ease and speed of writing
  • lightness
  • relative simplicity (compared to java)
  • npm (node ​​package manager (a huge number of libraries that can be installed on one line)
  • each library falls into the dependency tree and this is all done easily
  • continuous development (TypeScript is being actively developed (which brings typing, decorators to JS and is used, for example, for Angular)

Minuses:

  • flexibility and rapid development also generates disadvantages as you need to constantly monitor updates, some things come out insufficiently tested;
  • there was a case when a developer removed his library from NPM and many applications using it stopped working;

Advantages and disadvantages of Java


In contrast, immediately consider the main features of Java.

Pros:

  • speed of work
  • prevalence (in universities of many countries they study java, it is also convenient to study OOP in java),
  • huge set of libraries.

Minuses:

  • heaviness
  • some Java paradigms have been created for a long time and are already outdated,
  • JDK is proprietary, so Java is developing slowly.

Recently, JS has begun to overtake Java (and the farther, the more).
Java also leaves the Android world, it is being replaced by Kotlin which, although it uses the JVM, is still a different language.

Oracle and Google conflict


image

Java was created by Sun, which was later acquired by Oracle and is still owned by it. For this reason, for many companies using Java creates some problems.

Google was having problems when Oracle started a lawsuit with them for using Java in Android. Because of this, Google very actively adopted Kotlin, which appeared independently. Java is proprietary. But there is an Oracle virtual machine, as well as an open Java virtual machine (open JVM), which is used in Linux and written in open source. Sometimes there are some incompatibilities, but recently they are less and less.

By the way, Google was not able to completely abandon Java. In Dalvik, which is used as the core in Android, the JVM is embedded. Perhaps they will leave this, but it will be very difficult to do this because almost the entire Android ecosystem is built on Java - primarily on the use of a modernized JVM. And that, at some point, was also the reason for the conflict between Oracle and Google, because Oracle forbids just updating the JVM. This is the most important part of Java. And the language itself can be used with almost no restrictions.

Java vs node.js: performance and resource consumption


First of all, it is worth noting that Java performance is much higher than that of JS, and, accordingly, node.js.

image
Performance node.js and Java

If you run some simple task, such as squaring, then in tests the indicators can vary up to 10 times. If you run loops in millions of calculation tasks, Java will almost always surpass node.js. Plus, the huge difference between Java and node.js is that node is single-threaded, which is both an advantage and a disadvantage on the other hand.

Java can work with streams that are supported at the OS level, and it turns out that a program written in Java makes the most of the OS's capabilities. And if you need to write a highly loaded application that will use a large number of calculations, then Java will definitely be better for this. The problem is that even a small server written in Java will take up a lot of memory - both on disk and online.

Node.js is lightweight due to its event-driven architecture. It is built to work as a web server and does a very good job of servicing lightweight tasks. For example, a simple query like calculating something, or writing to a database, happens very quickly. And if there are a lot of requests and we want to scale the system in node, you can use the Nginx or Apache web server. You can have many identical node instances. Then everything will be distributed through round-robin load balancing. If we run 8 node instances on 16 cores, respectively, the OS itself will distribute the instances between the kernels. Node does not control this, it will have one thread.

Flow control in Java and node.js


In Java, we can create an application and run 8 threads in it. Due to the fact that there is a closer interaction with the OS, you can distribute the load.

As you know, one of the web servers written in Java is tomcat. There you can clearly see that when the user makes a request, additional threads are launched. And when the request comes to the node, the event loop will be processed and sent back, then the next request will come. And due to the fact that we do not expect the results of the first, it will also be picked up. While the requests are lightweight, everything is fine. However, when a heavy calculation is performed, if there is one instance, the node stops and a timeout occurs.

image
Java thread management

On node, you can write just a few lines of code and get the simplest web server. Naturally, for a wider functionality, where there will be notifications, authorizations, logging, etc. it is more difficult to implement, but there are frameworks that allow you to solve such issues.

image
Flow control in node.js

Java has a developed API - concurrency api, which allows you to work with competitive threads. But at the same time, this is one of the problems since competitiveness is a very complicated thing and not every developer is well versed in this.

The web, the REST API is the element of the node, and sometimes they use it. But if we are dealing with complex calculations, it is still better to use Java.

My java project


In Java, I had an interesting project - a distributed application, the main task of which was to process large amounts of graphic information for further use in directories. When creating a catalog, you need to prepare sets of a large number of images of various resolutions that will be used to create the catalog. Simply put, this is an application for automating prepress catalog preparation.

Photographers used to have to do everything manually. First you had to use some small application in order to upload your images. Further, the specialist creating the directory had to develop the directory structure through another application. Then, in another application, a workflow was created that threw pictures into the structure that was created. In general, the process was quite difficult. Used ImageMagick which is on Linux, Windows, MacOS. We were dealing with Linux.

For example, a .tiff picture 200-300 mb in size was loaded into the application, and from it it was necessary to make pictures of various resolutions, cut something, or make a substrate.
The first version of the application could not cope with the heavy load, even a server with 16 core processors was missing. We have improved the application architecture for using several instances at the same time so as not to fundamentally change the application. Many instances were launched that interacted with each other and each of them processed a piece of the task. It was difficult, but we were able to successfully implement everything in just a couple of months. And the system still works. In the process, we had to deal with competition and various aspects of interaction.

Something from this project could still be ported to node, but some things would still have to be done in Java, because there were many different calculations. Basically, we could make parts on node that would invoke certain parts in Java and use a microservice architecture. You could use a mixed version. But this approach does not always work, because a developer specializing in node may not be a specialist in Java and vice versa. And finding universal developers is much more difficult.

From experience on node.js


There was a project to organize a large amount of data. Something similar to the project described above. Only here we upload a file that contains a large set of information and is subject to validation through a third-party service (written in java), several times and according to different rules. It was necessary to process hundreds of gigabytes of information, and node was not intended for this.

It was especially interesting to design the system architecture, as The application consisted of several microservices, including third-party ones. When working with a third-party service that performed validation, we used RabbitMQ message broker. We gave the necessary information to a third-party server and received a message from RabbitMQ after the validation ended, then the data was processed in parts to avoid out of memory.

And if initially the application processed a file containing 10,000 records, now it can process up to a million. We still managed to solve this problem using node.js, although in Java it could be solved more easily, however, the customer wanted to use exactly node, because I needed a unified infrastructure and architecture with microservices written in JS. Using node to solve the problem was much more complicated and required more time, but node.js wins due to scalability. That is why now we can increase the number of workers and process more and more data. In Java, this would be more complicated.

In fact, any problem can be solved this way and that, but it’s worth repeating here: if there are a lot of calculations, it is better to use Java, if there are not many calculations, you can safely use node.

Summary and prospects: will node.js be able to overtake Java?


Now it comes to the fact that node.js will often be used as a wrapper, and the stuffing will be written in other languages. Its shortcomings have long been known. For example, a conditional flaw such as single-threading has already been fixed. The latest version of node introduces the ability to use multiple threads.

Java was originally created as a lightweight solution replacing C ++, and now has become heavyweight. It is like evolution. Maybe someday there will be something that will replace node.

image
Modules - Java development vs node.js

Now, by the number of orders, and according to my feelings, node.js is already ahead of Java.
JS is actively developing and it will change - maybe something will come to replace it.
Now there is no potential competitor that could replace Java and node.js.

The problem is that Java development has been rather slow lately, and node.js is developing at such a speed that it is not possible to replace it in the near future.

Read Next