Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use Google's CDN for jQuery?

People also ask

Should I use a CDN for jQuery?

There are a lot of people that say you should always use a CDN for libraries like jQuery (and other popular projects, as well). They say this for good reason. Using a CDN, as noted already, can reduce latency and allow browsers to cache a common file so it doesn't even have to load it from a server.

What are the advantages of using CDN in jQuery?

The advantages of using jQuery CDN are as follows: Decreasing the load on your website since the jQuery file will be loaded from a CDN and not from your website. The CDN will reduce the load on your website by loading the data from CDN and not directly from the website.

What is Google CDN jQuery?

Google provides CDN support for jQuery via the googleapis.com domain. The latest version of Google CDN provides four different types of jQuery versions- normal (uncompressed), minified, slim, and slim & minified. Google CDN provides the jQuery via ajax.googleapis.com domain.

What is the best jQuery CDN?

The best-performing CDN depends a bit on your needs. If you don't need HTTPS support, the fastest CDN is actually the official jQuery CDN, provided by Media Temple. Google's Libraries API CDN is a good second choice after that. If you need support for HTTPS, your best option is Google's Libraries API CDN.


This is because:

  1. It increases the parallelism available.
    (Most browsers will only download 3 or 4 files at a time from any given site.)

  2. It increases the chance that there will be a cache-hit.
    (As more sites follow this practice, more users already have the file ready.)

  3. It ensures that the payload will be as small as possible.
    (Google can pre-compress the file in a wide array of formats (like GZIP or DEFLATE). This makes the time-to-download very small, because it is super compressed and it isn't compressed on the fly.)

  4. It reduces the amount of bandwidth used by your server.
    (Google is basically offering free bandwidth.)

  5. It ensures that the user will get a geographically close response.
    (Google has servers all over the world, further decreasing the latency.)

  6. (Optional) They will automatically keep your scripts up to date.
    (If you like to "fly by the seat of your pants," you can always use the latest version of any script that they offer. These could fix security holes, but generally just break your stuff.)

There are several scenarios when you might not want to use jQuery from Google's CDN:

  1. When you are building an intranet application where the web server is hosted on the same network as the clients. If you use Google's CDN jQuery, you will be making a call to the internet rather than a webserver on the local network. This increases bandwidth for your organization, and is slower.

  2. When you want to run your application offline. (Quite linked to the first issue) If you need to work on a development environment (managed for example with Bower), you might need to be able to make your application work without any internet connection (ie: in a train :)

  3. When you need to customize it. For example if you use Grunt to build the library in order to use only certain modules or setting the AMD name

  4. When you are serving pages over SSL that require jQuery. You should serve the JavaScript over SSL as well as your page to avoid security problems and warnings.

Also, Microsoft hosts jQuery on their CDN. That is another choice comparable to using Google's hosted jQuery.


This study by TJ VanToll convinced me that it's better to concatenate jQuery with other scripts rather than load it from a CDN.

The reason is the latency involved in fetching jQuery on mobile devices:

"In 2012 the average RTT time on a mobile network in the United States was 344ms. And that 344ms applies to not only every HTTP request – which the average web page now makes 93 of – but also every DNS lookup and TCP connection... While average RTTs are improving, there are only small additional gains to be had, as current networks are within a small factor of the theoretical limit dictated by physics."

He also cites this post from Steve Souders that shows why you're actually unlikely to get the caching benefit from using a CDN:

"Because of the fragmentation in CDN providers, jQuery versions, and protocol usage (http vs. https), the chances of getting a CDN cache hit are shockingly low – and downloading from an external domain has the potential to perform not one, but three round trips (a DNS lookup, a TCP connection, and an HTTP GET)."


The greatest benefit is from caching. The theory is that if a visitor visited a site that was loading their JavaScript libraries, say jQuery for example from the Google CDN, then when they visit your website, the library is already in that user’s browser cache and will not have to be downloaded again. This sounds great in theory.

The benefits being shared here and elsewhere are all theoretical. Just came across an in-depth analysis of using a CDN and if it provides the expected performance benefits. http://www.root777.com/appdev/does-using-google-libraries-api-cdn-give-you-performance-benefits


A major reason NOT to let Google host your jQuery, one that many people don't think about, is that it won't download if you are in China. It's blocked along with many other scripts, fonts etc... hosted by Google CDN's. If you need to reach a Chinese audience you're best to always use a fall back hosted on your own server. Google APIS Blocked in China