Compilers and interpreters · Part 30 of 36
JIT compilers and optimisations
Expert 18 min
How V8 and other JITs tier up from interpreter to optimised machine code, the classic optimisations, and how to write code engines like.
What you will learn
- You can describe tiered compilation in a modern JavaScript engine.
- You can name classic optimisations such as inlining, constant folding and dead-code elimination.
- You can explain deoptimisation and why stable types help performance.
Before this: Bytecode and virtual machines
Tiered compilation
A JIT compiler faces a trade-off: optimising takes time, and most code runs only a few times. So modern engines use tiers and promote code only when it gets hot. Google's V8 (Chrome, Node.js, Deno) has four as of 2026:
- Ignition — an interpreter for bytecode; starts instantly and collects type feedback (which types each operation sees).
- Sparkplug (2021) — a very fast non-optimising compiler that turns bytecode into machine code almost one-to-one.
- Maglev (shipped in Chrome in 2023) — a mid-tier optimising compiler: quick to compile, uses type feedback.
- TurboFan — the top tier: slow to compile, aggressive optimisations for the hottest functions.
Firefox's SpiderMonkey and Safari's JavaScriptCore use similar tiered designs. Java's HotSpot has done tiered JIT compilation for decades.

Speculation and deoptimisation
The type feedback says, for example, "p.x has always been a small integer on objects shaped like {x, y}". The optimiser speculates that this stays true and generates code without generic checks — just a cheap guard. If the guard ever fails (a string arrives), the engine deoptimises: it throws the optimised code away, reconstructs the interpreter state and continues there. Correctness never depends on the speculation; only speed does.
That's why stable code is fast code in JavaScript:
- Create objects with the same properties in the same order (engines group them into hidden classes, also called shapes or maps).
- Don't mix types in one array or one variable without need.
- Avoid adding and deleting properties on hot objects.
Classic optimisations
These appear in JITs and ahead-of-time compilers alike (GCC, LLVM which powers Clang, Rust and Swift):
| Optimisation | Idea |
|---|---|
| Constant folding | compute 60 * 60 * 24 at compile time → 86400 |
| Inlining | replace a call with the function body; enables further optimisation |
| Dead-code elimination | drop code whose result is never used |
| Common subexpression elimination | compute a * b once if it appears twice |
| Loop-invariant code motion | move calculations that don't change out of the loop |
| Escape analysis | an object that never leaves a function can live in registers instead of the heap |
| Register allocation | keep hot values in CPU registers |
Optimisers work on an intermediate representation (IR), often in SSA form (static single assignment: each variable is assigned exactly once), which makes data flow explicit and many optimisations simple.
Measure, don't guess
Performance intuition is often wrong. Use a profiler (browser DevTools, node --cpu-prof, Python's cProfile, perf) to find the real hot spots, benchmark with realistic data, and remember the order of wins: a better algorithm (lesson "Big-O") beats a better data structure, which beats micro-optimisations.
Beyond: WebAssembly and AOT
WebAssembly (expert module "Modern tooling") gives the browser a compact, typed bytecode that engines compile to machine code quickly with predictable performance — no speculation needed for most of it. And ahead-of-time compilation is coming to managed platforms too (Java's GraalVM native images, .NET Native AOT), trading peak JIT speed for fast start-up and lower memory.
Key points
- JITs compile hot code while the program runs, in tiers: interpreter → baseline → optimising.
- V8 tiers: Ignition, Sparkplug (2021), Maglev (2023), TurboFan.
- Optimisers speculate on observed types and deoptimise when a guard fails.
- Classic optimisations: constant folding, inlining, dead-code elimination, CSE, loop-invariant motion, escape analysis.
- Keep object shapes and types stable; profile before you optimise.
Flashcards
- Question: What is deoptimisation?
- Answer: Discarding optimised code when a speculative assumption fails and continuing in a lower tier.
- Question: What is constant folding?
- Answer: Computing constant expressions at compile time.
- Question: What is SSA form?
- Answer: An IR where every variable is assigned exactly once.
- Question: Why do objects with the same property order run faster in JavaScript?
- Answer: They share a hidden class, so property access can be optimised.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
Order V8’s tiers from first to top.
Show the answer
Ignition → Sparkplug → Maglev → TurboFan
Code starts in the interpreter and is promoted as it gets hot.
Source: V8 blog, Maglev
An optimised function assumed integers and suddenly receives a string. What does the engine do?
- Deoptimise and continue in a lower tier
- Crash
- Return a wrong result
- Convert the string to 0
Show the answer
Deoptimise and continue in a lower tier
Guards detect the violated assumption and trigger deoptimisation; results stay correct.
Source: V8 blog, Maglev
Match the optimisation with its effect.
Show the answer
Constant folding = 60 * 60 computed at compile time; Inlining = A call replaced by the function body; Dead-code elimination = Unused computations removed
These are classic compiler optimisations.
Source: Cooper & Torczon, Engineering a Compiler
An IR in which each variable is assigned exactly once is in ___ form.
Show the answer
SSA
SSA = static single assignment.
Source: Cooper & Torczon, ch. 9
Sources
- V8 blog: Maglev — V8’s fastest optimizing JIT, V8 team, 2023
- V8 blog: Sparkplug — a non-optimizing JavaScript compiler, V8 team, 2021
- Engineering a Compiler (3rd ed.), Cooper & Torczon, Morgan Kaufmann, 2022
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.