📜 ⬆️ ⬇️

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

Recently, my colleagues and I discussed the popularity of some technologies - in particular, Java and node.js. After a brief Internet surfing, it turned out that these technologies are used by many information giants to develop and maintain their sites in the network. Below, I will give only a small part.

Companies using Java:

image
')
Companies using node.js:

image

No less interesting is the fact that according to the search data on indeed.com ( 06.28.2019 ) by Java Developer (30272 jobs) and node.js developer (7401 jobs) requests, specialists in these technologies are quite in demand.

image

But all this is only general information regarding popularity. Information that prompted me to delve into the topic and speculate about the technical features that led to the writing of this article.

Why they should be compared


Java is a language, node.js can be called an ecosystem built on the basis of JS, and, above all, on the basis of the V8 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 the execution environment . In the case of Java, it is a virtual machine. In the case of node.js, this is the V8 engine that is present on most operating systems, such as Windows, Linux, MacOS, and less well-known.

Developers can write code using the same language, and this will work more or less the same way on different operating systems due to the fact that there is a runtime environment. The execution environment influences how it interacts with the OS. In addition, they can be compared because they are used to solve a similar range of tasks .

V8 and JVM


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

There are prerequisites to the fact 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 transpiling 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 emergence of new versions of node.js is associated with the appearance of a new version of the V8 engine. They are directly interrelated.

Node.js: advantages and disadvantages


Node.js was created by Ryan Dahl in 2009.

The node.js itself includes several main components:


We turn to its pros and cons.

Pros:


Minuses:


Advantages and disadvantages of Java


In contrast, immediately consider the main characteristics of Java.

Pros:


Minuses:


Recently, JS is beginning to overtake Java (and the farther, the more).
Java also leaves the Android world, it is replaced by Kotlin who, although using the JVM, is still a different language.

Oracle and Google conflict


image

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

Google had problems when Oracle started a trial with them for using Java on Android. Because of this, Google has 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 lately there are fewer and fewer.

By the way, Google was not able to completely abandon Java. In Dalvik, which is used as the core in Android, the JVM is sewn. Perhaps this will go away, but it will be very difficult to do so. Virtually the entire Android ecosystem is built on Java — above all — on the use of an upgraded JVM. And this, at some point, was also the cause of the conflict between Oracle and Google, because Oracle prohibits modernizing the JVM just like that. 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 intensity


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

image
Node.js and java performance

If you run some simple task, like squaring, then in tests the indicators can differ up to 10 times. If you run loops in millions of costing tasks, Java will almost always exceed node.js. Plus, the huge difference between java and node.js is that node is single-threaded, this is both its advantage and its disadvantage on the other hand.

Java can work with threads that are supported at the OS level, and it turns out that a program written in Java makes the most of the OS features. And if you need to write a high-load application that will use a large number of calculations, then Java will definitely work 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 operational.

Node.js is lightweight due to an event-based architecture. It is built to work as a web server and copes very well with 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 into a node, you can use the Nginx or Apache web server. You can have many identical node instances. Then everything will be distributed through load balancing on round-robin. If we run 8 node instances on 16 cores respectively, the OS itself will distribute the instances between the cores. Node does not control this, it will have one thread.

Thread Management 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 started. And when the request for node arrives, the event loop will be processed and sent back, then the next request will come. And due to the fact that we are not waiting for the results of the first, it will also be picked up. While the requests are lightweight, all is well. However, when a heavy computation is performed, if there is one instance, the node stops and a timeout occurs.

image
Java thread management

You can write literally a few lines of code on a node and get a simple 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
Thread management in node.js

Java has a well-developed API - concurrency api, which allows you to work with competitive threads. But at the same time, this is one of the problems as Competitiveness is a very difficult thing and not every developer understands this well.

Web, REST API is the element of node, and sometimes it is used. But if we are dealing with complex cost estimates, it’s still better to use Java.

My Java project


In Java, I had an interesting project - a distributed application, the main task of which was processing large volumes of graphical information for further use in directories. When creating a catalog, it is necessary to prepare sets of a large number of images of various resolutions that will be used when creating the catalog. Simply put, this is an application for automating prepress catalog preparation.

Previously, photographers had to do everything manually. To begin with, it was necessary to use some small application in order to upload your images. Further, the specialist who created the catalog had to develop the structure of the catalog through another application. Then, in another application, vorkflow was created, which scattered the pictures into the structure that was created. In general, the process was quite difficult. Used ImageMagick which is on Linux, Windows, MacOS. We dealt with Linux.

For example, a .tiff image with a size of 200-300 mb was loaded into the application, and from it you had to make pictures of various resolutions, cut something out, or make a background.
The first version of the application could not cope with a large load; even a server with a 16-core processor was not enough. We have improved the architecture of the application to use several instances at the same time, in order not to radically change the operation of the application. Many instances started up that interacted with each other and each of them processed a piece of the task. It was difficult, but we managed to successfully implement everything literally in a couple of months. And the system is still working. The process had to deal with competitiveness and various aspects of interaction.

Something from this project could still be transferred to the node, but some things would still have to be done in Java, since There were a lot of different calculations. Basically, we could make parts on the node that would call certain parts on Java and use the microservice architecture. It was possible to use a mixed version. But this approach does not always work, because a developer specializing in node may not be a Java expert and vice versa. And finding universal developers is much more difficult.

From experience on node.js


There was a project for organizing 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.

Designing the system architecture was especially interesting, since 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 third-party server the necessary information and received a message from RabbitMQ after the end of validation, then the data were processed in parts to avoid out of memory.

And if the application initially processed a file containing 10,000 entries, it can now process up to a million. We still managed to solve this problem with the help of node.js, although in Java it could have been solved more simply, but the customer wanted to use exactly node, since I needed a single infrastructure and architecture with microservices written in JS. Using node to solve the problem was much more difficult, and took 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 is worth repeating: if there are a lot of calculations, then it is better to use Java, if there are not many calculations, you can safely use node.

Results and prospects: can node.js overtake java?


Now goes 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, such a conditional flaw as single-threading has already been fixed. The latest version of node provides the ability to use multiple threads.

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

image
Modules - Java vs node.js development

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

The problem is that Java is developing quite slowly lately, and node.js is developing at such a rate that it is not possible to replace it in the near future.

Source: https://habr.com/ru/post/460125/


All Articles