JIT compilation
- Node.js course
- Expert
- Lesson 2/47
- 5 min
Learn V8 JIT compilation and TurboFan.
JIT threshold logic
V8 looks at the call count to mark a function hot. When the call count crosses a threshold V8 applies more aggressive optimization. The threshold varies by V8 version but the idea is the same: frequently run code earns more optimization.
An isHot(callCount) function models this logic: return callCount >= THRESHOLD. THRESHOLD is a constant, say 100. isHot(150) returns true, isHot(50) returns false. This helps you understand V8's decision with a simple rule. Real V8 thresholds are more complex (deoptimization, oscillation) but the core idea is that hotness depends on call frequency.
Hot and cold functions
V8 separates functions into cold (rarely called) and hot (frequently called). Cold functions run with fast bytecode; hot functions are promoted to machine code. This spends compile time only on code that earns it.
Example: if isHot(50) returns false the function is cold; if isHot(150) returns true it is hot. V8 optimizes hot functions with type feedback: it watches which types flow through and generates machine code accordingly. If types change (say a string arrives where a number was expected) V8 deoptimizes. This is why code with stable types runs faster.
Practice Write the JIT threshold function
Define const THRESHOLD = 100. Write function isHot(callCount) { return callCount >= THRESHOLD; }. Print console.log(isHot(150)) and console.log(isHot(50)).
Starter code
// Define a threshold constant
// Write isHot: returns true if call count exceeds the threshold
// Print two calls (150 and 50)
// write hereSample solution
const THRESHOLD = 100;
function isHot(callCount) {
return callCount >= THRESHOLD;
}
console.log(isHot(150));
console.log(isHot(50));Expected output
true false
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the threshold condition
return callCount >= ____;
return callCount >= THRESHOLD;
Explanation: callCount is compared against THRESHOLD.
2 Match each concept to its description
| Term | Meaning |
|---|---|
Hot function |
Promoted to machine code |
Cold function |
Runs as bytecode |
Type feedback |
Watches types |
Deoptimization |
Reverts optimization |
3 What does isHot(50) return when THRESHOLD=100?
const THRESHOLD = 100;
function isHot(c) { return c >= THRESHOLD; }
isHot(50)- 50
- false Correct answer
- true
- 100
Explanation: 50 < 100 so it returns false.