JavaScript on GraalVM
Tags: javascript, graalvm, node
Published: 2018-10-01 12:38:23

---
title: JavaScript on GraalVM
published: true
description: Running JavaScript on the server side does not necessarily mean nodejs
tags: javascript, graalvm, node
---

When we think of JavaScript on the server side, our minds always think `nodejs`, but is this correct? What are the alternatives? Why do we need/care about these alternatives?

JavaScript on the server side has become mainstream, like sliced bread, everyone knows it, everyone has tried it, everyone uses it (Uber, Netflix even Microsoft...).

Node has made all this possible. It is a simple but not to simple runtime, that gives you enough to build powerful apps.

On my job I consider myself a polyglot developer, so I'm surrounded by `Java`, `JavaScript` and other languages all the time and this gives me exposure to different runtimes, `JVM`, `V8`, `ChakraCore` and most recently [GraalVM](http://www.graalvm.org/). According to their website:

> GraalVM is a universal virtual machine for running applications written in JavaScript, Python 3, Ruby, R, JVM-based languages like Java, Scala, Kotlin, and LLVM-based languages such as C and C++.

Why would one care about another runtime? Well, to me it all started when I saw the Techempower Benchmark and noticed that in contrast to popular believe, JavaScript isn't really super fast! (I believe it is super productive, but speed is highly debatable). Of course benchmarks should always be taken with a grain of salt, this one is quite interesting as it compares around 300 different frameworks across different languages and runtimes.

![techempower](https://thepracticaldev.s3.amazonaws.com/i/lp2po9c2odbj0la0wzkw.png)

To my despair, the best result of `nodejs` ranks at #52, so I decided to see what can we do to improve this. Looking at the benchmark source code turned out to not help much as there were no obvious performance bottlenecks, so I've decided to look outside of the <span style="text-decoration:line-through">box</span>/`V8`.

[![graaljs-performance](https://image.slidesharecdn.com/jazoon2014slides-141031103831-conversion-gate02/95/graal-vm-multilanguage-execution-platform-14-638.jpg)](https://www.slideshare.net/ThomasWuerthinger/jazoon2014-slides)

So at the first sight, `GraalJS` does not seem to give us much of an improvement, a few wins here and there, but on the other hand, what the Techempower benchmarks is showing is that, on the long run, jobs on the JVM tend to be the fastest.

In order to verify my premise, I've started working on a small project called EcmaScript for [Eclipse Vert.x](http://vertx.io/) or [ES4X](https://reactiverse.io/es4x) in short. The idea was simple:

> Create a modern EcmaScript runtime on top of the Eclipse Vert.x toolkit using the capabilities of the JVM/GraalVM

In order to keep the project maintainable I've decided to keep it minimal:

1. Provide a **`commonjs`** module loader
2. Develop using a node/npm style a.k.a. **`package.json`**
3. Support debugging with chrome inspector
4. IDE support using **TypeScript Definitions**

After a couple of iterations and feedback, I've decided to implement the bechmark. So I've started with:

```sh
npm init -y
npm add vertx-scripts --save-dev
npm add @vertx/core --save-prod
npm add @vertx/web --save-prod
npm add @reactiverse/reactive-pg-client --save-prod
npm install
```

This looked pretty much `nodejs` friendly to me, so I've [implemented](https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/frameworks/JavaScript/es4x/index.js) the benchmark and went over the process of being reviewed by the community and being merged into the master branch. And when that happen something amazing emerged:


![bench-db](https://thepracticaldev.s3.amazonaws.com/i/avcuo8l2emaly6mu57e5.png)

`JavaScript` and `Vert.x` were not just **fast**, but by far the **fastest** when compared to `nodejs` (or `Spring Framework` as a reference to the most common framework used in Java), but even faster that the pure `Java` counterpart of the benchmark using the same dependencies `vert.x` and `vertx-web`.

In fact the results are amazing as it puts `JavaScript` as the **#2** fastest language in the benchmark:

![es4x-fastest](https://thepracticaldev.s3.amazonaws.com/i/h5s3h0le6t4urhbsokqz.png)

So the next time you need to write high performance JavaScript server code, give a look at [ES4X](https://github.com/reactiverse/es4x), or join the project and help making it even greater!!!