Tool to automatically inline JavaScript function calls?

十年热恋 提交于 2019-12-01 14:23: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!


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/6009411/tool-to-automatically-inline-javascript-function-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!