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!

2
  • 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. Commented May 15, 2011 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. Commented Oct 8, 2012 at 20:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

JIT engines and static analysis tools (like closure-compiler) take into account the scope change impacts when deciding to inline a function.
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.
"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.
-1

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.

5 Comments

-1 This doesn't really answer the question. Inlining can be beneficial in some situations, such as inside an iteration in a main loop.
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
People coming from Facebook, please don't downvote @Ivo Wetzel. That's called vote brigading and it is not a good thing.
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".
came here from google looking for how to inline functions in javascript. context: malicious code analysis. i do think that the question deserves a fuller answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.