pwray
pwray

Reputation: 1125

Automatic javascript function inlining

This is a question about the typical behaviour of the JIT compiler in modern javascript engines. Lets say I have a class A with many fields, instances of which which are heavily used from another class B, including within loops. Rather than expose the internals of A, there are a bunch of one-line access methods. Individually, each method will make little difference to performance, but let's assume that collectively they make a big difference. Will a modern JIT inline these functions?

Upvotes: 3

Views: 445

Answers (1)

doubleOrt
doubleOrt

Reputation: 2507

Late answer, but I think this may help future viewers of the question.

It depends on the complexity of the methods, a side-effecting function may not be inlined, while a simple method will usually be inlined.
However, as @Ry- mentioned in the comments, it is not truly predictable. JavaScript engines take many things into consideration before making an optimization -- to see whether or not it is actually worth it, so there is no objective answer to your question.
The best thing to do is to take your code and profile it to see whether or not the functions are being inlined, this article also shows another way of doing this, if you are serious enough.

Upvotes: 1

Related Questions