Output
Source
Machine code
Interpret
Compile
Execute
Before the execution and only once
During execution and every time
Output
Source
Machine code
Interpret
Compile
Execute
During execution and only sometimes
Executed enough number of times
These are only examples!
Compilation
Deopt
V8’s interpreter, Ignition, collects profiling information about that function while interpreting it. Once the function becomes hot, this information is passed to V8’s compiler, TurboFan, which generates optimized machine code.
When the profiling information is no longer valid — for example because one of the profiled objects gets a different type during runtime — the optimized machine code might become invalid. In that case, V8 needs to deoptimize it.
If we don't pay attention, our code may be very JIT unfriendly.
function f(o) {
return o.x
}
// Monomorphic - great!
f({ x: 1 });
f({ x: 2 });
// Polymorphic - OK.
f({ x: 3, y: 1 });
// Megamorphic - bad!
f({ x: 5, a: 1 });
f({ x: 6, b: 1 });
f({ x: 7, c: 1 });