Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cache file or one more HTTP Request?

on all the "speed up your website" sites and books they always tell us to minimize HTTP requests at all costs. This is fine and nice but what if that means that on every page you have to reload 120kb again and again and again because the user cache is empty?

If i use 5 js files on every page of my website, wouldnt it be better to put them in one file and load this file on every page instead of putting them together with all other variable files in one big file and save one HTTP request. From which point or filesize on is it ok "cache" a file and have another HTTP request?

I give you an example of 3 pages when i use only one HTTP request for one minifed JS file per page:

  1. jquery, jquery ui, thickbox, lavalamp menu => together minified in one file = 300kb
  2. jquery, jquery ui, cycle plugin => together minified in one file => 200kb
  3. jquery, jquery ui, galleria plugin => together minified in one file => 250kb

And now the other possibility with always 2 HTTP requests: One File consisting of jquery and jquery ui => 150kb, lets call it "jui.js" for now

  1. jui.js, thickbox, lavalamp = again 300kb in the beginning, BUT now jui.js is cached for the other 2 pages
  2. (jui.js is cached now so not loaded), only cycle plugin => only 50kb to load but one more HTTP request as i load jui.js and the cycle plugin seperately.
  3. (jui.js is already cached), only load galleria plugin => only 100kb more to load but again 2 HTTP requests where one request is already cached

So at which point or Kb size is it ok to have another HTTP request on a normal "responsive" web server?

Does anybody have any best practices or is it just "Minimize HTTP requests at all costs!"?

(I hope i made myself clear :) And i will vote up people as soon as i have some points!)

EDIT:

It is basicly a simpler question: How long does a extra HTTP roundtrip for a cached js file need? If the http request is slower than the time i would need to download the extra non cached parts on every page, then i would put everything in 1 big file on every page(1 different big file on every page).

If the HTTP request for a cached js file is nearly nothing, then i would split the parts that every page needs in an extra js file(minifed of course) and include the dynamic parts of every page in differend(again minified) js files.

So if on most pages i need a 100kb extra(the dynamic part), how do i test the time for a cached HTTP request? Are there any numbers, did anybody test something like this already?

Thanks for the great answers already!

like image 591
Tschef Avatar asked Jan 30 '26 04:01

Tschef


2 Answers

This is big complex subject. They write whole books on this subject ;)

For resources (javascript, css etc) it is sometimes better to download them individually. The browser will download them in parallel. if page a needs resources x y z but page b only needs x and z, separating them out is a good thing. Other times a resource that is needed on every page might be better downloaded all at once. It depends.

But with javascript, the browser downloads the JS first before it renders the page (if the script tag is in the head section) so you would see better performance if you add a defer attribute, or include at the bottom of the page, and trigger your javascript with a body=onload.

Remember too you can set caching headers on resources so the browser will cache them in memory or disk. This makes a huge difference in many cases.

There are really no hard and fast rules, just some guidelines. Your best bet is to test! what works better over dialup doesn't work as well over broadband.

Fiddler is a nice program that will show you your loading times if you are on a modem for instance.

like image 87
Byron Whitlock Avatar answered Feb 01 '26 16:02

Byron Whitlock


in short, there is no rule of thumb here. Depending on your webserver settings you may want to try optimizing by merging files to one larger file... I know apache could be configured to use same connection to stream several files. Best thing to do is use a benchmarking tool such as apache AB to simply test your changes.

As for jquery stuff though, you may include your scripts from a publicly located domain such as google to 1) avoid connections 2) many people have them cached in browser already.

ie: http://code.google.com/p/jqueryjs/

like image 42
Mohammad Avatar answered Feb 01 '26 16:02

Mohammad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!