News

Let Google Host jQuery

If you’re a developer, you’ll often see websites hosting their own JavaScript (jQuery) library.  They don’t have to.  Instead, we urge you to use Google’s  content delivery network (or CDN) to serve jQuery (and other JavaScript libraries) to your users.   Why?  Doing so has some real advantages.  Let’s look at just the top 3. 

If all you want is the link, here it is, or check the Developer library for all of them:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

NOTE: This post is only about WHY you should be using Google’s jQuery CDN.  This post was inspired by Dave Ward‘s article, “3 Reasons why you should let Google host jQuery for you,” which deep-dives into various implementations too complex to be covered here.

01. Decreased Latency

A CDN distributes your static content across servers in various, diverse physical locations. When a user’s browser resolves the URL for these files, their download will automatically target the closest available server from that diverse network.  Here’s a better explaination from MaxCDN.

In the case of Google’s CDN, what this means any users not physically near your server will download jQuery faster than if you force them to download it from your server which could be located anywhere.

There are a handful of CDN services from hosting providers comparable to Google’s, but it’s hard to beat the price of free and the speed of Google!

02. Increased parallelism

To avoid needlessly overloading servers, browsers limit the number of simultaneous connections. Depending on the browser, this limit may be as few as 2 connections per hostname.

Using Google’s jQuery CDN eliminates at least one request to your site, allowing more of your local content to downloaded in parallel.  That means a faster website and faster is always better in digital.

03.  Better caching

Last, but certainly not least, is if your users have already cached jQuery from Google’s CDN, they don’t have to load it at all!

If you’re hosting jQuery locally then your visitors must download it at least once. Each of your visitors has probably already viewed sites using identical copies of jQuery, but those copies of jQuery are ignored when they visit your site (different hostnames).

However, when a browser sees references to CDN-hosted copies of jQuery (same hostname), it understands all of those references refer to the exact same file. Thus, it skips downloading it entirely.

CDNs, such as Google’s, creates a potent “cross-site caching” effect which all sites using the CDN benefit from.  Since Google’s hosted version of jQuery serves the file with headers that attempt to cache the file for up to one year, this effect truly has amazing potential.

The Proof is in the Pudding

According to a recent study, Google will consume 16.5% of all consumer Internet capacity in the United States during 2008.   I think it’s fair to say that they know how to efficiently serve up some content.

So, why aren’t you using Google’s CDN?

Comments

Saddened, 01/03/2013

(comment via email): “I’m saddened to see it appears you borrowed http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/ without permission or attribution. I contacted the original author, Dave Ward. He’s aware his article gets taken without permission or attribution, but is still upset to see it happen. I teach programming and stumbled across this while trying to find Dave’s original article. As a respectable business, you should know better.”

Justin Downey, 01/03/2013

Thanks for the heads up, @Saddened.

See the Yellow alert box added to the top of the post with a hats-off to Dave, the inspiration for the article.

Justin Downey, 06/02/2014

I would also mention that because we’ve removed the “http:” from the link it will work seamlessly via secure (https) and insecure (http) connections.

You DO have to update your reference if you want to use newer version of jQuery. Alternatively, look into jQuery’s own hosted library. That source is “code.jquery.com/jquery-latest.min.js”.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2014 JDM Digital