Sub topic 3
What Just-in-Time Compilation Means
This chapter assumes you can write JavaScript functions and run scripts with Node.js, and that you know the basic difference between compiling a program ahead of time and interpreting it. The previous chapter showed how V8's Ignition interpreter runs bytecode. Here you will see why engines don't stop at interpretation, and what it means to compile code while it runs.
The problem with interpreting everything
An interpreter runs each bytecode instruction through a dispatch loop. Every time it executes a + b, it checks the types of a and b, picks the right kind of addition, and moves on to the next instruction. That flexibility is what JavaScript's dynamic typing needs. It is also slow when the same line runs millions of times with the same types.
A C compiler avoids this cost by translating the program to machine code before it runs. JavaScript can't simply copy that approach, for two reasons:
- Source code arrives over the network and must start running quickly. A long compile step would delay page load.
- Types are not known until the program runs. An ahead-of-time compiler would have to generate code that handles every possible type, which loses most of the benefit.
Compiling at runtime
Just-in-time (JIT) compilation means the engine compiles code to machine code while the program is running. It compiles only the parts that turn out to matter. The engine starts by interpreting everything. As it interprets, it counts how often each function runs and records which types it sees. When a function becomes hot, meaning it runs often enough to be worth the effort, the engine compiles it to machine code. That machine code is tailored to the types the engine actually observed.
| Approach | When code is compiled | Knows runtime types? | Startup cost |
|---|---|---|---|
| Interpreter | Never (bytecode only) | Checks them on every operation | Low |
| Ahead-of-time compiler | Before the program runs | No | None at runtime, but compiling happens beforehand |
| JIT compiler | During execution, for hot code | Yes, from profiling | Paid only for hot code |
Seeing it happen
The --trace-opt flag makes V8 log when it decides to optimize a function.
// Run with: node --trace-opt hot.js
function square(n) {
return n * n;
}
let total = 0;
for (let i = 0; i < 1000000; i++) {
total += square(i);
}
console.log(total);The output includes lines that mention square being marked and then compiled for optimization. The exact wording changes between Node versions. If you shrink the loop to 10 iterations, those lines disappear, because square never gets hot.
The common misconception
Developers often assume a JIT compiles the whole program at startup, or that compiled code is final. It is neither. The engine compiles selectively. Its machine code also relies on assumptions taken from past behavior, so if those assumptions stop holding, the engine can throw the machine code away. Later chapters cover the tiers that make this gradual, and the deoptimization that undoes a failed bet.
Exercise
Why can a JIT compiler sometimes produce faster code for a JavaScript function than an ahead-of-time compiler could?
Show answer
The JIT compiles after watching the function run, so it knows which types actually reach it. It can generate machine code specialized for those types. An ahead-of-time compiler would have to handle every possible type.
Exercise
A script calls a function exactly once at startup. Would you expect V8 to JIT-compile it to optimized machine code? Why?
Show answer
No. A function that runs once never becomes hot, so the cost of compiling it would outweigh any gain. The interpreter runs it from bytecode.