admin管理员组文章数量:1332352
We have about 30 external JavaScripts on our page. Each is already minified.
To reduce HTTP requests and page load time, we are considering bining them to a single file. This was remended by the YSlow tool.
Is this wise, or is it better to bine them into, say, two files with 15 scripts each?
Is there an optimal size for the bined JavaScript files?
We have about 30 external JavaScripts on our page. Each is already minified.
To reduce HTTP requests and page load time, we are considering bining them to a single file. This was remended by the YSlow tool.
Is this wise, or is it better to bine them into, say, two files with 15 scripts each?
Is there an optimal size for the bined JavaScript files?
Share Improve this question asked Jul 20, 2010 at 15:35 frankadelicfrankadelic 20.8k37 gold badges114 silver badges167 bronze badges 2- Do you need all 30 files on every request? – Gumbo Commented Jul 20, 2010 at 15:37
- yes, assume all files are needed – frankadelic Commented Jul 20, 2010 at 16:00
2 Answers
Reset to default 9The fewer the HTTP requests, the better. If you want your page to work on Mobile devices as well, then keep the total size of each script node under 1MB (See http://www.yuiblog./blog/2010/07/12/mobile-browser-cache-limits-revisited/)
You might also want to check whether any of your scripts can be deferred to after onload fires. You could then make two bined files, one that's loaded in the page, and the other that's loaded after page load.
The main reason we ask people to reduce HTTP requests is because you pay the price of latency on each request. This is a problem if those requests are run sequentially. If you can make multiple requests in parallel, then this is a much better use of your bandwidth[*], and you pay the price of latency only once. Loading scripts asynchronously is a good way to do this.
To load a script after page load, do something like this:
// This function should be attached to your onload handler
// it assumes a variable named script_url exists. You could easily
// extend it to use an array of scripts or figure it out some other
// way (see note late)
function lazy_load() {
setTimeout(function() {
var s = document.createElement("script");
s.src=script_url;
document.body.appendChild(s);
}, 50);
}
This is called from onload, and sets a timeout for 50ms later at which point it will add a new script node to the document's body. The script will start downloading after that. Now since javascript is single threaded, the timeout will only fire after onload has pleted even if onload takes more than 50ms to plete.
Now instead of having a global variable named script_url
, you could have script nodes at the top of your document but with unrecognised content-types like this:
<script type="text/x-javascript-deferred" src="...">
Then in your function, you just need to get all script nodes with this content type and load their srcs.
Note that some browsers also support a defer
attribute for script nodes that will do all this automatically.
[*] Due to TCP window size limits, you won't actually use all the bandwidth that you have available on a single download. Multiple parallel downloads can make better use of your bandwidth.
The browser has to interpret just as much javascript when it is all bined into one monolithic file as it does when the files are broken up, so I would say it doesn't matter at all for interpretation and execution performance.
For network performance, the less HTTP requests the better.
本文标签: performanceCombining JavaScript files as recommended by YSlowoptimal sizeStack Overflow
版权声明:本文标题:performance - Combining JavaScript files as recommended by YSlow - optimal size? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322592a2453088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论