Do you/should you load jQuery from Google CDN?

10 replies
If you load jQuery from the local server there is no chance of waiting for the 3rd party site, but little chance the end-user already has it cached in their browser.

Some people are convinced getting it "free" from Google is faster, others say "can be extremely slow", and there is also the option of getting it from jQuery's site.

Does anyone know if the jQuery.org CDN uses Amazon web services, and/or do they have a "perfect record" of up-time?
#cdn #google #jquery #load #you or should
  • Profile picture of the author oldschoolwarrior
    I was just creating a php javascript loader, and found that the using googles compressor, really is slow.

    Closure Compiler Service

    Was talking about 30 seconds to load, and minimize code.
    I download jquery, and ran it though my own php loader, to use caching and gz compressions, and it works great now!
    {{ DiscussionBoard.errors[7645109].message }}
  • Profile picture of the author Tony Martinez
    I load jQuery from Google's CDN, but with a local fallback in the odd event that Google's copy doesn't load. The following is a model of what I have in my HEAD. I load the script from CDN first, then immediately check if jQuery is defined. If it's not, then my local fallback loads. Probably not the best fallback solution, but it works for me.

    Code:
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script>window.jQuery||document.write('<script src="http://yourdomain.com/path/to/jquery.js"></script>')</script>
    {{ DiscussionBoard.errors[7647776].message }}
  • Profile picture of the author wayfarer
    Just load it from Google. If it's good enough for Twitter, it's good enough for me.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[7647917].message }}
  • Profile picture of the author Damien Roche
    Just to offer another option, I minimise and concatenate all javascript (and css) into a single js/css file.
    Signature
    >> Seasoned Web Developer (CSS, JavaScript, PHP, Ruby) <<
    Available for Fixed Fee Projects and Hourly ($40/hr)
    {{ DiscussionBoard.errors[7651843].message }}
  • Profile picture of the author SteveSRS
    depends if you are using your own CDN...

    If yes don't... only creates extra DNS request which is a bad thing..
    If no.. depends test it.. I actually usually load it from my own domain.
    {{ DiscussionBoard.errors[7658704].message }}
  • Profile picture of the author Don Art
    No question you should load from Google (second choice from jquery).

    Do not load from your host.

    Here's why with proof:

    if your visitor's browser follows http1.1 recs it will only open 2 simultaneous connections to any one host name at a time - so multiple hostnames is actually better:

    "Clients that use persistent connections SHOULD limit the number of simultaneous connections that they maintain to a given server. A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy. A proxy SHOULD use up to 2*N connections to another server or proxy, where N is the number of simultaneously active users. These guidelines are intended to improve HTTP response times and avoid congestion."

    Loading content from multiple hosts is generally better for another reason, if a server is slowish, content is still able to be fetched via the connections to the other specified hosts.


    From Yahoo's Performance Best Practices:

    "Reducing the number of unique hostnames has the potential to reduce the amount of parallel downloading that takes place in the page. Avoiding DNS lookups cuts response times, but reducing parallel downloads may increase response times. "

    Note too: The issue of dns lookup time is mitigated by the fact that the user or user's ISP is extremely likely to have cached the ajax.googleapis.com domain. More from the same source:

    "DNS lookups are cached for better performance. This caching can occur on a special caching server, maintained by the user's ISP or local area network, but there is also caching that occurs on the individual user's computer. The DNS information remains in the operating system's DNS cache (the "DNS Client service" on Microsoft Windows). Most browsers have their own caches, separate from the operating system's cache. As long as the browser keeps a DNS record in its own cache, it doesn't bother the operating system with a request for the record."

    Next:

    CDN technology is seriously robust and proven. Google's tech is 2nd to none. It's entire business depends on their CDN.

    Google uses it's massive CDN to deliver content from servers worldwide. There will be an edge server closer (therefore probably faster) to your visitor than your own server is likely to be.

    Finally and I think most decisive:

    The user's browser is exceedingly likely to have already cached Google's copy of jquery from a previous visit to one of the millions of sites using Google's hosted apis. This means the browser will just load a local copy - far faster than any network access.
    {{ DiscussionBoard.errors[7665483].message }}
    • Profile picture of the author Happy_Balance
      Originally Posted by Don Art View Post

      N
      if your visitor's browser follows http1.1 recs it will only open 2 simultaneous connections to any one host name at a time - so multiple hostnames is actually better:
      ...
      Good point about multiple hostnames, I should remember to load some images from a sub-domain.
      Signature

      Every Day Is Fun! :)

      {{ DiscussionBoard.errors[7761863].message }}
  • Profile picture of the author Michael71
    Depends on the files you are loading after jQuery.

    I prefer minifying/combining all JS files into one file.

    In that case you just need one http-request and that's from your own domain.
    Will save DNS lookup and time, same with CSS.
    Signature

    HTML/CSS/jQuery/ZURB Foundation/Twitter Bootstrap/Wordpress/Frontend Performance Optimizing
    ---
    Need HTML/CSS help? Skype: microcosmic - Test Your Responsive Design - InternetCookies.eu

    {{ DiscussionBoard.errors[7762311].message }}
    • Profile picture of the author AppDeveloper
      I agree with the others suggesting loading it from Google.

      The vast majority of people will have it cached via that url. I don't have specific numbers, but with the number of sites linking to the Google version, I would be surprised if less than 95% of your visitors didn't have it cached.

      And the ones that don't have it cached are going to most likely load it quicker from Googles CDN than from your server.

      Combining all your JS files into one request is definitely a good idea, however, when that file gets too large (greater than ~64k) it makes sense to split it up into two files. If the rest of your included JS libraries are large then this can add up quickly. Modern web browsers will download multiple JavaScript files in parallel. So having two 60k files will download faster than one 120k file.
      {{ DiscussionBoard.errors[7764705].message }}

Trending Topics