11

Inlining JavaScript function calls speeds up the execution and also reduces the code size after gzipping, as described in this article:

http://blog.calyptus.eu/seb/2011/01/javascript-call-performance-just-inline-it/

However, I can't find a tool which automatically processes a JS source file and inlines all (or better, selected) inlinable function calls in it. Google's Closure Compiler does some inlining, but not always, and nor is it configurable.

Thanks in advance!

Gary Chang
  • 1,042
  • 12
  • 18
  • 2
    You don't need to inline everything, and it will make reusing the code a lot more difficult. I would suggest you just use something like the Google Closure Compiler which does perform a number of optimisations (including inlining) and makes the code smaller. – Adam Heath May 15 '11 at 16:29
  • 3
    This is a great question deserving better answers. When working with repetitive tasks inlining often makes a huge difference. Such as continuously processing pixels or calculating noise. – bennedich Oct 08 '12 at 20:28

2 Answers2

1

I hardly believe that this "technique" speeds up any execution time. At least not in a real-world scenario. The Blog might be right about code-size & Gzipping tho.

Anyway, I don't think any Javascript minification/compressor will do this alot. The reason is simple and very obvious in the example provided. By replacing the function call with the actually function code, you're setting things into another context. This might end up very evil. What if the parent function(-context) already declares and uses a variable named foo. If the same variable is used within the other function you might overwrite that and cause errors.

Even worse if there is some use of try/catch or eval blocks which creates an additional context with a carefully expressed "dynamic scope" (which is actually not available in ecma-script). However, in this scenario it's pretty much impossible for a JIT or any Javascript implementation to optimize anything.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 2
    JIT engines and static analysis tools (like closure-compiler) take into account the scope change impacts when deciding to inline a function. – Chad Killingsworth Feb 17 '17 at 19:52
  • Sure, but the "solution" for all decisions if `try/catch` or `eval` is involved within a context comes down to "don't do anything" afaik. – jAndy Feb 20 '17 at 11:15
  • "An inlining tool might break your code if it doesn't work correctly" is not really a sensible objection. You should assume that any mature inlining tool works for its intended purpose, just like you assume that your compiler compiles your code correctly or your interpreter runs it correctly. – kaya3 Jan 07 '22 at 06:35
0

Let the JIT figure things like inlining out fr you. Inlining can easily worsen the performance by killing cache performance.

Also, unless you have identified the actual bottlenecks, doing premature optimization like this is hardly ever worth it.

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • 4
    -1 This doesn't really answer the question. Inlining can be beneficial in some situations, such as inside an iteration in a main loop. – Petah Sep 14 '16 at 11:58
  • 8
    Same. Question: *how do I solve X*. Answer: *don't solve X*. StackOverflow: *brilliant*! Really? You're not OP to know its motives. Or the motives of someone Googling that question, actually needing the answer. -1 – MaiaVictor Feb 17 '17 at 18:33
  • People coming from Facebook, please don't downvote @Ivo Wetzel. That's called vote brigading and it is not a good thing. – MaiaVictor Feb 17 '17 at 22:39
  • If you rephrase this answer just slightly then it blatantly answers the question: "yes, there is a tool to automatically inline Javascript functions, it is called the JIT, and you are already using it". – kaya3 Jan 07 '22 at 06:30