DEV Community

Cover image for How JavaScript Compilation Works
Masroor Hosseini for Arshida

Posted on

How JavaScript Compilation Works

JavaScript is one of the most widely used programming languages, primarily because of its role in web development. It was initially an interpreted language, which means that the browser would read and execute JavaScript code line by line. However, with the evolution of modern JavaScript engines, the process has shifted toward compilation and optimization. In this article, we'll explore how JavaScript compilers work, focusing on the concepts behind the compilation process.

Interpreted vs. Compiled Languages
Before diving into the details of JavaScript compilation, it's important to understand the difference between interpreted and compiled languages:

Interpreted Languages: Code is executed line by line by an interpreter, without converting it into machine code ahead of time. This allows for dynamic behavior but often results in slower execution.
Compiled Languages: Code is translated into machine code before it is executed. This generally results in faster execution as the CPU can directly understand the machine code.

JavaScript sits in the middle ground. Historically, it was interpreted by browsers, but modern engines, like Google's V8 (used in Chrome and Node.js), have introduced Just-In-Time (JIT) compilation to improve performance.

JavaScript Engine: The Core of Compilation
JavaScript compilers are part of what is called a JavaScript engine. Each browser has its own JavaScript engine:

V8: Google Chrome and Node.js
SpiderMonkey: Mozilla Firefox
Chakra: Microsoft Edge (before moving to Chromium)
JavaScriptCore: Safari

All these engines implement the ECMAScript standard, which defines how JavaScript should behave. Let's look at the steps a typical JavaScript engine takes to execute code.

How JavaScript Compilation Works
Parsing the Source Code The first step in the compilation process is parsing. The engine breaks down the JavaScript code into an Abstract Syntax Tree (AST) through two phases

Lexical Analysis (Tokenization): The JavaScript code is split into small chunks called tokens. Each token represents basic elements like keywords, variable names, operators, etc.
Syntax Analysis: The tokens are then organized into a tree-like structure called the Abstract Syntax Tree (AST). This tree represents the hierarchical structure of the program.

let x = 10;

The above code would be broken down into tokens like let, x, =, and 10, and then arranged in the AST to understand how the variable x is assigned the value 10.

  1. Intermediate Representation (IR) After building the AST, the engine converts it into an Intermediate Representation(IR). This is an abstract machine-level code that is easier for the engine to optimize. The IR serves as a bridge between the source code and machine code, helping to apply various optimizations before final execution.

3.Just-In-Time (JIT) Compilation Modern JavaScript engines use a technique called Just-In-Time (JIT) compilation to optimize performance. JIT compilers take parts of the code and compile them into machine code right before they are needed. This provides the benefits of both interpreted and compiled languages.

Baseline Compiler: A baseline JIT compiler initially compiles the JavaScript code to machine code quickly, without heavy optimization. This allows for fast execution but may not be the most efficient.
Optimization and Deoptimization: The engine then monitors the performance of the code during runtime. If it notices frequently executed code (also called "hot" code), it further optimizes that portion by applying advanced techniques like inlining functions or reducing redundant operations.
Deoptimization: If the assumptions made during optimization turn out to be wrong (for example, a variable was assumed to always be a number, but later becomes a string), the engine can deoptimize the code and revert it to a less optimized version.

  1. Garbage Collection JavaScript engines manage memory automatically through a process known as garbage collection. This process identifies objects that are no longer in use and frees up memory. Modern engines use strategies like Mark-and-Sweep and Generational Garbage Collection to efficiently manage memory, making sure the application runs smoothly without memory leaks.

Example: V8 Engine
Let's take a look at how Google’s V8 engine implements this process.

Ignition: V8 uses a component called Ignition to generate bytecode from JavaScript. Bytecode is a lower-level representation of the source code, which is still abstract but easier to execute than raw JavaScript.
Turbofan: If some part of the bytecode is executed frequently, the V8 engine uses its optimizing compiler, Turbofan, to further compile this bytecode into highly optimized machine code.
Inline Caching: Another technique V8 uses is inline caching, which remembers the types of objects and operations in frequently executed functions. This helps in optimizing the code by making fewer assumptions about the code's behavior, leading to faster execution.

Key Optimizations in JavaScript Compilation

Inlining: Replacing function calls with the function's body to reduce overhead.
Type Specialization: Making assumptions about variable types to generate more efficient code.
Dead Code Elimination: Removing code that is never executed.
Lazy Compilation: Compiling only the parts of the code that are actually used.

Conclusion
JavaScript’s shift from a purely interpreted language to one that relies heavily on JIT compilation has significantly improved its performance. Modern JavaScript engines like V8 combine multiple techniques to parse, optimize, and execute code efficiently, making it possible for JavaScript to run complex applications in browsers and server environments. Understanding how these engines work gives developers insight into writing more efficient, optimized code that makes the most of the engine's capabilities.

Top comments (0)