What Is Function Inlining?
You'll learn how JavaScript engines eliminate function call overhead by replacing calls with the actual function code.
The Core Idea
Function inlining is an optimization where the JavaScript engine replaces a function call with the actual body of the function itself. Instead of jumping to a function, executing it, and returning, the engine just drops the function's code directly into the calling location.
Think of it like a recipe reference: instead of saying "see page 42 for the sauce recipe" (a function call), you copy the sauce instructions directly into your main recipe (inlining). You save the time of flipping pages back and forth.
Why Inlining Matters
Every function call has overhead:
- Push arguments onto the stack
- Jump to the function's location in memory
- Set up a new execution context
- Execute the function
- Clean up and return
For small, frequently-called functions, this overhead can dwarf the actual work being done. Inlining eliminates all of it.
What Gets Inlined
Modern JavaScript engines (like V8) inline functions automatically when:
- The function is small (typically under a certain bytecode size)
- It's called frequently (identified as a "hot" function)
- The call site is monomorphic (always calls the same function shape)
You don't control inlining directly—it's the engine's decision based on runtime profiling.
The Connection to Earlier Concepts
Remember monomorphic vs polymorphic sites? Inlining works best when the engine knows exactly which function will be called. Polymorphic calls prevent inlining because the engine can't inline multiple possibilities.
Key Takeaway: Function inlining replaces function calls with the function's actual code to eliminate call overhead—an automatic optimization that works best with small, frequently-called, monomorphic functions.