Divick
Divick

Reputation: 1273

Combining javascripts vs splitting across CDNs

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).

Upvotes: 0

Views: 136

Answers (2)

Danniel Magno
Danniel Magno

Reputation: 304

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.

Upvotes: 0

Dennis
Dennis

Reputation: 14455

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)

Upvotes: 1

Related Questions