When V8 Inlines Functions
You'll learn which functions V8 actually chooses to inline and why.
The Inlining Sweet Spot
V8 doesn't inline every function—that would bloat your code unnecessarily. Instead, it follows a pragmatic strategy: small, hot functions called from monomorphic sites are prime candidates for inlining.
Let's break down each criterion:
Small Functions
V8 has size limits for inlining. If a function's bytecode is too large (typically over ~600 bytes), it won't be inlined regardless of how often it's called. Short, focused functions are your best bet.
Hot Functions
"Hot" means frequently executed. V8's profiler tracks which functions run most often. A function called once won't be inlined, but one called thousands of times in a tight loop becomes a prime target.
Monomorphic Call Sites
This is critical: the call site (where you invoke the function) must be monomorphic—meaning it always calls functions with the same hidden class signature. If your code calls calculate(obj) and obj sometimes has shape A and sometimes shape B, that's polymorphic, and V8 becomes reluctant to inline.
Why This Matters
Think of it like a restaurant kitchen. You'll only memorize the recipe (inline) for dishes that are:
- Simple to make (small)
- Ordered constantly (hot)
- Always made the same way (monomorphic)
If a dish changes ingredients every time or is rarely ordered, keeping the recipe in your head isn't worth it—you'll just check the book each time (regular function call).
Key Takeaway: V8 inlines small, frequently-called functions that are invoked from monomorphic call sites. Keep your hot-path functions tiny, use them often, and pass consistently-shaped objects to maximize inlining.