In this guide I’m going to cover if NodeJS is faster than Java in 2023. Since things have changed a lot over the inception of both programming languages I’m going to focus on what the situation is today. For reference Javascript was released approximately a year before Java back in 1995 vs 1996 which makes both frameworks pretty mature by now.
None of them compare to languages such as C/C++ but they do share some similarities between them. My goal in this guide is to focus specifically on how they compare in terms of speed in various operations such as computational tasks, I/O, concurrency and memory access.
Let me start by saying that I’ve used both Java and NodeJS in various projects and I like both for different reasons. But lets see which is faster Java or NodeJS for your every day needs.
Basic Computation Speed Comparison
I’m going to start first with the basic computational needs between Java and NodeJS. Since this is the most common thing most people encounter when doing day to day programming I thought it has the highest significance and I’d like to cover it first before I do a deeper dive into the more specifics of the two.
Let’s start by saying fundamental difference between Java and NodeJS which will help you understand why Java outperforms NodeJS in most tasks. The difference is that Java is essentially a pre-compiled language. This means that the code you write gets translated by the interpreter into byte code in an intermediate machine language that gets translated by an engine called the JVM (Java Virtual Machine).
So you’d ask me why is this important? The answer is that when you pre-compile something it will always be much faster than something that runs and gets translated in real time which is NodeJS. This fundamental difference applies very well in computational tasks but lets see this through with an example.
In both of the examples I basically assum the program is basically printing out a variable’s value.
In Java this would look something like this:
Here is an example of how you can print a variable with value 33 in Java: public class Main { public static void main(String[] args) { int variable = 33; System.out.println(variable); } }
In NodeJS this would look something like this:
() => { let variable = 33; console.log(variable); }
But what happens behind the scenes you would ask me? Well lets take a deeper dive into the entire workflow for each case and we will be able to understand why Java outperforms NodeJS in terms of speed.
When I have a piece of code this goes through the following stages until it gets executed in Java vs NodeJS:
As you can see above the part where the code gets translated into byte code before it gets executed is missing in the NodeJS case. This means the NodeJS v8 VM has to translate it in real time (as it executes). The overhead for this is massive and can add a lot of delay in the execution time.
In contrast Java does this job before the code gets executed and in fact it’s able to catch problems with it too rather than dealing with it in runtime. This means the access of the variable we are seeing above is already in machine code when it’s running but in NodeJS it’s in text format which has to translate into bytes before it executes.
Now imagine performing this operation for every single line of code, you will understand the overhead is going to grow linearly over time resulting into NodeJS being slower than Java due to this architectural difference. In practise when I use both you do not really notice the difference until you start doing a lot of computational stuff such as mathematics etc. In that case since every basically operations is real-time in NodeJS takes much longer to complete than it does in Java.
Below I’m going to cover some other use-cases and explain how NodeJS performs to Java in other areas.
Concurrency Speed Comparison
Lets start with saying that Java has the Java Executor framework and the java.util.concurrent package. This provides tools for creating and managing threads, as well as synchronizing access to shared resources.
On the other hand Node.js uses a single-threaded, event-driven model for concurrency, in which incoming requests are handled asynchronously by a single thread and a queue of tasks is maintained. This allows Node.js to handle a large number of concurrent connections with a minimal amount of overhead, but it can make it more difficult to write complex concurrent programs in Node.js compared to Java.
To be a bit more specific lets see examples of how each works and then go over why Java outperforms here NodeJS.
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { public static void main(String[] args) { // Create a fixed thread pool with 2 threads ExecutorService executor = Executors.newFixedThreadPool(2); // Submit two tasks for execution executor.submit(new Task("Task 1")); executor.submit(new Task("Task 2")); // Shutdown the executor executor.shutdown(); } } class MyTask implements Runnable { private String printme; public MyTask(String printme) { this.printme = printme; } @Override public void run() { System.out.println("Running " + printme); try { Thread.sleep(900); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Finished " + printme); } }
And in NodeJS:
const { Worker, isMainThread, parentPort } = require('worker_threads'); if (isMainThread) { // This code is running in the main thread const worker = new Worker(__filename); worker.on('message', (msg) => { console.log(msg); }); worker.postMessage('main thread'); } else { // This code is running in the worker thread parentPort.on('message', (msg) => { console.log(msg); }); parentPort.postMessage('worker thread'); }
A few observations here:
- NodeJS is much more complicated when it comes to writing the concurrency code
- The NodeJS worker framework is still a wrapper that runs over Javascript (we will go over later why this is bad)
- Java supports true threading under the scenes making it super lightweight and fast compared to NodeJS
To better understand and give you a clear view of how things work I created a diagram on the NodeJS VM vs the Java one which we will see a very important difference.
First lets look at how Java handles concurrency when it comes to affecting the speed of your programs execution.
As shown below basically Java creates pseudo threads in the code which essentially translate into native system threads giving true performance boost in the execution of your program. On the other hand lets see how the NodeJS VM handles threading.
As you can see above the main difference is that NodeJS uses a single thread for the entire execution. Yes this means all your functions will go through that single thread and they have to wait on a queue to execute. Since NodeJS implements this mechanism very efficiently it may not be as noticeable when being blocked by I/O operations however it does take a toll compared to the multi-threading paradigm that Java does more efficiently over NodeJS.
I/O Access Speed Comparison
Now that I covered basically the basic computational stuff on Java vs NodeJS I will jump right into some more specific tests which is input/output access.
I was expecting a somewhat neutral result and this is what I really got when I did some basic benchmarking. But let me break it down by case for you and explain why this is the case and we are not seeing massive speed differences here like computation and concurrency results.
Basically the NodeJS VM does a very good job here scheduling these asynchronous tasks properly. So let’s say if you are delaying or waiting for a response on a file access it will block and execute something else. Similarly if you are trying to do a network request that’s taking a few milliseconds to respond the VM detects it and allocates its thread/cpu time into something else. This makes basically NodeJS very efficient even when it runs on a single thread and there’s a minor or negligible difference compared to Java.
I do want to make something clear here and this is that if the Java developer is good and has segmented his code properly into threads and such then there’s no way the NodeJS application will be able to compete with this. For example if we consider a case where you are doing some stress testing in a server (btw I have a cool article on stress testing APIS here) where you need to run a lot of parallel connections at the same time then Java will beat NodeJS hands down in execution speed making it significantly faster. This has a big catch though, the code has to be properly segmented in threads that it is enough to make a difference. In my experience this is very rarely the case as most Java developers do not know how to do this properly, making the end result more over the same in both cases.
Memory Access Speed Comparison
Similarly to the I/O access memory access works in a very similar manner. Both NodeJS and Java are pretty much on par in these kind of executions due to the fact that memory is not really blocked and it’s a form of input/output access. The reason I have it here as a special mention is that you need to understand a fundamental difference when it comes to threading.
Essentially the Java threads as shown in the diagram I created earlier run into the same memory context of the same process. This means when a fork or new process is created basically Java threads all share the same amount of memory!! This means there’s no advantage unless you go with multi-processing which runs into different cores. In that case both NodeJS and Java can do the same as they both can support different executing processes into the system.
In this case we have a tie of the two and I certainly believe in most development tasks you will not notice any speed variance between NodeJS and Java execution of your code.
NodeJS vs Java Speed Benchmark Results
Before I let you go I want to close with a test I implemented in both NodeJS and Java and executed to test them in the four points shown below:
- Memory Access
- I/O Access
- Computational/Calculations
- Concurrency
As you can see the four points in my graph represent those exact values. Basically you can tell that memory and I/O access has almost the same values in both programming languages Java and NodeJS. The difference really kicks in when we start to cover computational and concurrency. Java really outshines outperforming NodeJS in basically every concurrency test due to the fact that it supports real threading. Furthermore when it comes to computational stuff again Java is the clear winner in terms of speed of execution.
If you are hesitant on making a decision and you believe your team is fluent in both languages the safe bet to go with is Java. If you don’t really care much about concurrency and computational stuff then both will be great languages to have in your environment. My experience with both has been extensive and I can tell you that it’s always project dependent with off course Java taking the lead in most cases. The advances and ease of use of NodeJS sometimes is so good that I still pick it for some projects. If you are interested into seeing the code I wrote for the above tests drop me a note on my twitter and I will send that to you.
Hopefully I helped you make the right choice when it comes to comparing Java vs NodeJS and deciding which one is faster for your project.