0

Various website optimization guides suggest that if possible one should combine multiple javascripts into one to reduce the number of http requests. On the other hand there is slightly orthogonal suggestion that using CDNS like that of google and microsoft would split the load and browser would be able to download in parallel.

I am a bit confused as to should I combine all the javascripts including my own as well as third party js as a single javascript file or should I keep them separate (at least for those which are available via free CDNs)?

It is also unclear to me that do browsers download multiple javascrit files in parallel or they wait for each javascript to download before downloading others?

I have a website which uses javascript heavily and there are couple of libraries that I am using (jquery, jquery-ui, jquery.json-2.3, jquery.metadata.js, jquery.jcarousel.min.js jquery.validate.js, jquery.blockUI.js).

Divick
  • 1,213
  • 1
  • 20
  • 44

2 Answers2

1

Referencing a CDN is always a good idea. Arguing that the browser would have to do more http-requests is invalid, because this will only happen once. the file will be cached then and not be requested again. With CDNs, it is also possible that the user already has a cached version and will not have to request it at all (while when referencing your local version, the user cannot have cached that version before).

I would suggest to try to reference CDNs for every third-party-plugin available. For your own scripts and scripts that are not hosted by a CDN, try to minify the scripts and put them into a single file to reduce traffic load.

(for minifying, there are lots of online tools available, i.e. http://jscompress.com/ , which also lets you generate a single js file out of several ones)

Dennis
  • 14,210
  • 2
  • 34
  • 54
  • Thanks for the quick reply but I see the benefit of CDN only if multiple javascripts are downloaded in parallel by browser. If browser blocks for downloading every javascripts, it may not help (assuming that say the javascripts are not cached initially). Moreover I see that various CDNS have many versions of js libraries and the argument that there is a likelihood that some other website also using jquery will have it already cached for you, probably isn't valid. As far as caching goes, if it downloads my javascript wouldn't it also get cached for subsequent pages? – Divick Apr 30 '12 at 12:46
  • to answer your last question - yes, for subsequent requests your files would also be cached by the browser. Most CDNs also offer the possibility to always reference the latest version (google's cdn for example does this when you specify `1` as the version number). This helps against the argument that there might be different versions cached - by using the url to the latest version you increase the probability to reference a cached version. i would highly recommend using cdns, you save some traffic and your users might save some requests (as time goes by, cdns get referenced far more often). – Dennis Apr 30 '12 at 12:52
  • and yet about blocking - some browsers also support some 'guessing' approach, in which they assume that the script does not affect the dom and thus can be loaded in parallel. even without this approach, i still think you are better of with cdn's for the best mixture of performance, traffic and stability. – Dennis Apr 30 '12 at 12:54
  • >>google's cdn for example does this when you specify 1 as the version number). I am not clear as how you can specify this? – Divick May 01 '12 at 03:51
  • Nevermind, I got the answer here. http://stackoverflow.com/questions/441412/is-there-a-link-to-the-latest-jquery-library-on-google-apis – Divick May 01 '12 at 03:53
0

Browsers download multiple JS files since they're on different CDN. When they're on the same CDN, the browser wait each one to finish to download another one.

It's a good idea, keep third party scripts on different files and CDNs, when possible, and keep your own scripts minified, on a subdomain of your, making your CDN, to speed the load.

Danniel Magno
  • 304
  • 1
  • 3
  • 21
  • So are you suggesting that the browser will not download multiple javascripts from same domain in parallel while it will if the javascripts are from different domains? That sounds little weird though. Thinking more about it, I get a sense that browsers can download multiple javascripts on a page in parallel (from same of different domains, as long as the browser is not limited by number of parallel requests to same domain which is usually 2 or more). – Divick May 01 '12 at 03:55