Will V8 inline instance method calls?

36 views
Skip to first unread message

GregRos

unread,
Apr 16, 2025, 2:21:19 PMApr 16
to v8-users
I'm writing a low-level, CPU-bound parsing library, and I want to make sure the parsing code is as efficient as possible.

I know that V8 will sometimes inline function calls if they're "simple" enough, don't contain any blacklisted syntax, etc.

But will it do the same with instance method calls? For example, let's say I just have a very simple wrapper like this:

class X {
    arr
    constructor(arr) {
       this.arr = arr
    }
   
    push(x) {
       this.arr.push(x)
    }
}

Would a call like `x.push(1)` get inlined into `x.arr.push(1)`?

If so, what if I complicate things a bit:
  • If `push` instead calls another instance method. Would both calls get inlined?
  • If it's an inherited method up the prototype chain (but is never overriden)
  • If push calls a function in a variable, like if it received a function argument and called it. Would the body of push get inlined even if the internal call cannot be inlined?

Ben Noordhuis

unread,
Apr 16, 2025, 5:48:26 PMApr 16
to v8-u...@googlegroups.com
Depends. Probably, but...

The optimizing tiers are pretty good at inlining (it isn't called the
mother of all optimizations for nothing) but JIT compilers are, by
necessity, a bunch of heuristics and estimates, and they change all
the time. What works today need not work tomorrow.

In general, it's best to write your code in a straightforward manner
and not worry too much about performance upfront. V8 generally does a
lot better on mundane code than code that tries to be clever because
the former is its bread and butter. Only start thinking about tuning
when profiling shows hot spots.

GregRos

unread,
Apr 17, 2025, 8:33:19 AMApr 17
to v8-users
What does it depend on?
Reply all
Reply to author
Forward
0 new messages