Sub topic 1
From Source Text to Bytecode: The Engine Pipeline
A JavaScript engine never runs your source text directly. It turns that text into progressively faster forms and decides at runtime how much work each function deserves. This chapter uses V8, the engine in Chrome and Node.js, to follow that path.
Prerequisites
This chapter assumes you can:
- write JavaScript functions, objects, classes, and arrays
- explain the basic difference between compiled and interpreted programs
- run scripts with Node.js from the command line
- work comfortably with JavaScript's dynamic typing
The Pipeline
The parser reads source text and builds an abstract syntax tree (AST). V8 parses lazily. It fully parses a function only when the function is first called, so code that never runs costs very little. The bytecode generator then walks the AST and emits compact bytecode.
Running Bytecode in an Interpreter
Ignition is V8's interpreter. It is a register machine with an accumulator, and it executes bytecode one instruction at a time. Ignition starts quickly and uses little memory. While it runs, it also records which types each operation actually sees. You can print the bytecode for a function:
// Run with: node --print-bytecode --print-bytecode-filter=add add.js
function add(a, b) {
return a + b;
}
console.log(add(1, 2));Just-in-Time Compilation and Tiers
Just-in-time (JIT) compilation means producing machine code while the program runs, so the compiler can use information gathered at runtime. Compiling takes time, which means it only pays off for code that runs often. V8 therefore uses tiers:
| Tier | Kind | Trade-off |
|---|---|---|
| Ignition | Interpreter | Instant start, slowest execution |
| Sparkplug | Baseline compiler | Fast to compile, no optimization |
| Maglev | Mid-tier optimizer | Moderate compile cost, good code |
| TurboFan | Top-tier optimizer | Slow to compile, fastest code |
A function moves up a tier as it gets hotter, meaning as it runs more often. If optimized code relies on an assumption that later turns out false, V8 deoptimizes and execution returns to Ignition.
Hidden Classes: Giving Dynamic Objects a Fixed Layout
You can add properties to a JavaScript object at any time, but looking them up by name in a hash table is slow. V8 avoids this by attaching a hidden class (called a map in V8) to every object. The hidden class records which properties the object has and the memory offset of each one. Objects built the same way share a hidden class:
function Point(x, y) {
this.x = x;
this.y = y;
}
const a = new Point(1, 2);
const b = new Point(3, 4);
// a and b were built the same way, so they share a hidden class.
const c = new Point(5, 6);
c.z = 7;
// Adding z moves c to a new hidden class; a and b are unchanged.Exercise
Why doesn't V8 compile every function with TurboFan right away?
Show answer
Optimizing compilation is expensive. Most functions run only a few times, so the time spent optimizing them would exceed the time saved. Without runtime type feedback, TurboFan would also have nothing to base its speculative optimizations on.
Exercise
Do a and c from the Point example share a hidden class after c.z = 7? Why?
Show answer
No. Adding z moved c to a new hidden class that includes z, while a kept the original one with only x and y.