How GraalVM Runs High-Performance JavaScript on JVM

GraalVM executes JavaScript on the Java Virtual Machine (JVM) with performance rivaling modern dedicated engines like Google’s V8. It achieves this through a combination of the Truffle language implementation framework, dynamic Abstract Syntax Tree (AST) interpretation, GraalVM’s optimizing Just-In-Time (JIT) compiler, and speculative optimizations. This architecture allows JavaScript code to be transformed into highly efficient native machine code while seamlessly interoperating with Java and other languages with zero performance overhead.

The Truffle Language Implementation Framework

At the core of GraalVM’s JavaScript engine (GraalJS) is Truffle, an open-source library for building high-performance language interpreters. Instead of compiling JavaScript directly to JVM bytecode, GraalJS parses JavaScript source code into a Truffle-based Abstract Syntax Tree (AST).

In this model, each node in the AST represents an operation (such as addition, property access, or function calls) and acts as an executable interpreter. As the program runs, these nodes profile the runtime types of variables, continuously rewriting and specializing themselves based on observed execution patterns.

Partial Evaluation and Dynamic Compilation

When a section of JavaScript code is executed frequently (“hot code”), the GraalVM JIT compiler steps in using a technique called Partial Evaluation:

  1. Inlining the AST: The compiler treats the specialized Truffle AST as a single, combined compilation unit.
  2. Eliminating Interpretive Overhead: Truffle applies partial evaluation to inline the AST nodes into the underlying interpreter, effectively removing the interpretation layer entirely.
  3. Generating Native Machine Code: The Graal compiler converts the evaluated AST directly into highly optimized native machine code (x86 or ARM), bypassing standard JVM bytecode limitations.

Speculative Optimizations and Deoptimization

JavaScript is dynamically typed, which traditionally makes optimization difficult. GraalVM overcomes this through aggressive speculative assumptions:

Memory Management and Native Interoperability

Because GraalJS runs directly inside the JVM, it benefits from robust JVM infrastructure: