A theoretical question:
We all know about the pro's of minifying and combining javascript files in order to reduce HTTP requests to speed up a website. But when popular javascript libraries is used (jQuery for instance), it isn't too stupid to assume that these already have been downloaded to the clients computer from another page.
So what should be preferrered? How does the big guys in the industry handle it?
A) Combine and minify each script into a massive one and serve it from my own CDN.
B) Combine all "self-written" scripts into one file and utilize available CDN's of libraries where possible.
Thanks!
Generally, fewer HTTP requests mean a faster loading website. The loading speed of a website is now an important search engine ranking factor. On average, the media page loading speed for Google's 10 results is just 1.65 seconds. This highlights the importance of having a fast-loading website.
How Many HTTP Requests Should Web Pages Have? You should strive to keep the number of HTTP requests under 50. If you can get requests below 25, you're doing amazing. By their nature, HTTP requests are not bad.
The HTTP 429 Too Many Requests response status code indicates the user has sent too many requests in a given amount of time ("rate limiting"). A Retry-After header might be included to this response indicating how long to wait before making a new request.
"Combine and minify each script into a massive one and serve it from my own CDN"
If you have a CDN, what are we talking about? :) You mean server, right?
"How does the big guys in the industry handle it?"
The big guys always use their own servers.
"...it isn't too stupid to assume that these already have been downloaded to the clients computer from another page."
Unfortunately it is. Facts:
I think it depends on you site:
The best way to really know what to do is to test which combination of techniques saves you the the most traffic / connections.
P.S.: I personally do not like the idea to let other people serve files for my webpage, because what will happen if the CDN fails, but your server stays alive? If this is not a problem for you, try to server all libraries you use from a reliable CDN.
I think the best approach is to use a minified 'application.js' file (containing all application specific javascript) and then use a service such as Google AJAX Libraries API (found here) to load jQuery, Prototype, etc.
I think it comes down to a couple of things:
There's also a difference between using popular Javascript packages, such as jQuery, and using a personal package, which only visitors who have visited your site will have.
The performance enhancement may occur from two places: 1) browser cache and 2) dns cache, which even if the file isn't stored locally, the dns server has a route that minimizes the request time, or even temporarily serves the file.
I would advise using a CDN and hosting the files locally. Depending on your resources (hardware/bandwidth), you might need to use a CDN anyhow. It'd be nice to use server side schedulers to check on the CDN status and reroute the path when applicable.
Also, take a reminder that some users choose to turn off their browser cache. So minifying your JS is always a plus. You should separate your JS into two files: 1) Needed on Load and 2) Not needed on load. Basically, get the necessary code out there first, to improve the perceived load time. Then load all the other extras (eg slideshows, color changers, etc).
One last point is to make use of the Expires headers, since none of this is important if you don't optimize that. That is what will really reduce the speed for returned visitors with cache enabled. YSlow is a nice Firefox addon that will help evaluate your load performance.
To answer your question: Reduce HTTP requests, but do your own evaluation on the file size of the JS.
(Being Extreme) You don't want one 10MB JS file, or your site will take too long to load. Nor do you want 1000 10KB files, because of the HTTP overhead. Again, use this to illustrate the point that you want a balance between size and number of files - and as I said earlier, package them into performance needed vs wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With