tags:

views:

120

answers:

6

I understand that after a browser loads jquery, generally it is added to the browsers cache.

My site has hundreds of pages and I have a header file that is included on these pages and only some of these pages use jquery code so I am wondering will a page load faster or have better performance to the end user if I do not include jquery on pages that do not use it, or since it is cached, does it not affect performance at all?

+2  A: 

If there are landing pages (pages the user end up on when visiting your website for the first time, like your homepage) that don't require jQuery, don't include jQuery there.

The user experience would be better; he will perceive your website as being faster. Once jQuery is loaded once (while the user is browsing your website), it doesn't really matter anymore until he deletes his cache.

Make sure your server is sending correct caching headers, though.

Sinan Taifour
A: 

I would personally, because I know that the jquery file will be cached, and I just have scope creep and keep thinking the client will want some functionality on a page that currently doesn't have any - so overall one can't really lose if it's already embedded within a vast majority of your pages... assuming this page isn't a standalone/landing page which in that case I would probably exclude it.

meder
+3  A: 

If your jQuery files are properly cached it basically won't matter if you include them or not if you don't use them. Not to any significant degree anyway.

By "properly cached" I mean versioned with a far futures Expires header.

See Supercharging Javascript in PHP.

Basically if it makes sense not to include them then don't (obviously). But if it becomes tricky, hard or error-prone then don't bother. You don't want to find yourself in a position of getting error reports on pages that don't work when they're trying to use jQuery that doesn't exist because you thought the page didn't need it.

cletus
You still have a conditional get in such case, this does matter. You also have parsing time for jquery. It *does* matter.
Marcin
You don't have a conditional get. Don't confuse `Expires` with `If-Modified-Since`.
Robert Munteanu
@Marcin: It really doesn't. jQuery doesn't take much time to parse. If a page is taking a long time to render odds are it isn't jQuery that's the issue. And conditional gets aren't going to have a real noticeable affect either.
Spencer Ruport
Whether conditional gets are expensive or not isn't really relevant though because you don't need them and what I espouse above won't use them.
cletus
thanks for the link
jasondavis
A: 

Browser will have jquery in cache, but it will still make a conditional get to your server to check if it hasn't changed and shouldn't be updated. When parsing the <script> tag browser will block to do this conditional get (hopefully fast, but still 30-50ms) and then parse and execute jquery code (additional 10ms).

If you're fine with one additional request and 40-70ms slowdown for every other page that doesn't use jquery I think it's ok.

Marcin
+2  A: 

Depending on how your page is constructed, you might want to put the jQuery on the bottom of the page. In that case loading jQuery shouldn't affect visible performance at all and you should feel free to include it.

Jourkey
I might try this out thanks
jasondavis
+1  A: 

To sum up the various good points from the other answers, if you observe best performance practices such as:

  • Proper headers to ensure caching where available (Expires, Last-Modified, ETag, etc)
  • Proper placement of script tag (as close to </body> as possible)
  • Minification/gzip

The impact should be negligible in terms of user experience. In fact, depending on usage habits, inclusion may serve to help keep it in cache longer, and improve performance overall.

eyelidlessness