performance - Combining JavaScript files as recommended by YSlow - optimal size? -


We have around 30 external Java scripts on our page, each is already minified.

To reduce HTTP requests and page load times, we are considering combining them in a file, it was recommended by the YSlow tool.

Is it wise, or is it better to combine them into two files with 15 scripts each?

Is there an optimal size for this combined javascript files?

At least HTTP requests, better if you want your page to work on mobile devices too, So keep the size of each script node below 1 MB (see)

You also want to check that one of your scripts may later postpone or fire onload then you can create two composite files, which The page is loading, and the second page loads when loaded.

The main reason we ask people to reduce HTTP requests, because you pay the cost of the latency, each request is a problem if those requests are progressively If you can make many requests in parallel, then this is a better use of your bandwidth [*], and you only pay the cost of the latency once it is good for the asynchronous way to load the script. Rika.

To load a script after page load, do something like this:

  // This function assumes that your onload handler // is present in a variable named script_url You can easily use an array of scripts to expand it or see it in 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); }  

It is called onload, and later sets the timeout for 50ms, on which it will add a new script node to the body of the document. The script will then start downloading now. Now when Javascript is single threaded, the timeout will be completed only after the load, even if ONL load is more than 50 mms to complete.

Instead of having a global variable called script_url , script nodes may be at the top of your document, but with such unknown content-types:

  & lt; Script type = "text / x-javascript-deferred" src = "..." & gt;  

Then in your function, you need to get all script nodes with this content type and load your SARC.

Note that some browsers are also deferred for script nodes which will automatically do all of this.

[*] Due to the limit of TCP window size, you will not actually use all the bandwidth that you have available on single downloads. Multiple parallel downloads can make better use of your bandwidth.


Comments