Sub topic 4
Tiered Compilation: Trading Startup Time for Peak Speed
This chapter assumes you know what bytecode is and how V8's Ignition interpreter runs it, and that you understand the basic idea of just-in-time (JIT) compilation from the previous chapters. You should also be able to run Node.js with command-line flags.
The Core Trade-off
A JIT compiler has a hard choice to make. Fast machine code takes a long time to produce. Code that compiles quickly runs slowly. Most functions in a real program run only a few times, so spending a lot of compile time on them is wasted effort. A small number of hot functions, such as inner loops, request handlers, and parsers, run millions of times. For those, compile time spent once pays off on every later call.
Tiered compilation handles this by giving the engine several execution tiers. Every function starts in the cheapest tier. The engine promotes a function to a more expensive tier only after the function has proven it is hot.
V8's Four Tiers
| Tier | Kind | Compile cost | Code speed | Uses type feedback |
|---|---|---|---|---|
| Ignition | Bytecode interpreter | Very low (bytecode only) | Slowest | Collects it |
| Sparkplug | Baseline compiler | Low | Moderate | Collects it, does not speculate on it |
| Maglev | Mid-tier optimizing compiler | Medium | Fast | Yes |
| TurboFan | Top-tier optimizing compiler | High | Fastest | Yes, aggressively |
Sparkplug translates bytecode straight into machine code in one pass, with no intermediate representation. It mostly removes the interpreter's dispatch overhead. Maglev builds a real compiler graph and specializes the code using the feedback gathered so far, but it keeps its optimization passes cheap. TurboFan runs a full set of optimizations and produces the best code, at the highest cost. Maglev and TurboFan compile on background threads, so the main thread keeps running the current tier while the faster version is built.
How the Engine Decides to Tier Up
V8 gives each function a budget. Calls and loop back-edges use up that budget. When the budget runs out, V8 checks how much feedback the function has collected and may queue it for the next tier. A function stuck inside one long-running loop does not have to wait for its next call. On-stack replacement (OSR) swaps the running loop into optimized code while it is still executing.
The dotted arrows in the diagram show the way back down. Optimized code depends on assumptions, and when an assumption fails, execution returns to the interpreter. Later chapters cover that process in detail.
Watching Tiering Happen
// tier.js
function sumTo(n) {
let total = 0;
for (let i = 0; i < n; i++) total += i;
return total;
}
const start = process.hrtime.bigint();
let result = 0;
for (let k = 0; k < 2000; k++) result += sumTo(10000);
const ms = Number(process.hrtime.bigint() - start) / 1e6;
console.log(result, `${ms.toFixed(1)} ms`);Run it in three ways and compare:
node tier.js # all tiers enabled
node --jitless tier.js # Ignition only, no machine code generated
node --trace-opt tier.js # log each optimization decisionWith --trace-opt, you should see lines saying that sumTo is being compiled by Maglev and then by TurboFan, and some may mention OSR. The exact wording depends on the Node version. The --jitless run is usually many times slower, which shows how much the upper tiers contribute.
Exercise
Why would V8 add Maglev between Sparkplug and TurboFan instead of sending hot functions directly to TurboFan?
Show answer
TurboFan's compile time is high, so many moderately hot functions would run slow baseline code for a long time while waiting for it, or would never run long enough to repay the cost. Maglev produces good, feedback-specialized code quickly, which closes the gap between baseline and peak speed. TurboFan is then kept for the hottest code.
Exercise
You run a script with --jitless and it is only slightly slower than the default run. What does that suggest about the script?
Show answer
The script probably spends little time in hot JavaScript code. Its time is likely going to startup, I/O, native built-ins, or code that runs too few times to tier up, so the optimizing tiers have little to speed up.