views:

146

answers:

6

I remember reading somewhere that it is a lot better to load a JS framework from Google's AJAX Libraries API, rather than using a locally hosted one.

This means that instead of :

<script src="jquery.js"></script>

You load the framework from Google:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;

I think that the advantage was mostly about caching but I'm not sure about it.

Can someone explain to me the reason exactly on why it is better to load frameworks from Google instead of hosting them locally ?

+3  A: 

Firstly, it means the load is on Google's servers, rather than your own, which will save you both server processing time and bandwidth. Secondly, it's quite likely that Google's servers are faster than your own for the vast majority of internet users.

Plus, from Google's point of view, it lets them get more data about how people use the internet and their APIs.

ETA: Also, if Google update their APIs, it means you'll always be using the latest version. This may or may not be a good thing (bug fixes vs. back-compatibility).

me_and
I think the point about Google learning about how people use the internet is quite valid.
Vincent Ramdhanie
As regards Google updating their APIs, if you use a specific version (like I posted in my post), for example 1.3.2, it won't affect you unless you update your link manually yourself.
Andreas Grech
Fair point, I missed that!
me_and
+9  A: 

There is a good chance another site will also be using the same js files from Google's servers, so the file will already be cached by your browser and it won't need to download it again for your site.

Check out this article for some of the other benefits as well.

jamesaharvey
+1 If everyone took jquery from google, it would always be cached whatever site you went to!
Paul Creasey
I don't know about you guys and girls, but I'm going to go change all my sites right now.
BranTheMan
This is the most important point
Justin Johnson
A: 

It comes down to footprint: Google has a distributed set of network locations thereby ensuring a low latency delivery to almost everywhere.

So, if you get to serve as much as possible from Google, your customers QoE (Quality of Experience) will improve.

  1. Customers care about this because the faster they get their web page, the more than can get done.

  2. Google cares about this because the faster customers get their web page, the more they can serve per day and thus the more money$ they make (from advertising of course).

jldupont
+4  A: 

There are pros and cons to having Google host your javascript libs.

PROS:

  • Google's CDN is way better than yours
  • loads of data gathered by Google about your site and usage
  • the user may have the libs cached from another website

CONS:

  • 1 extra DNS look-up
  • loads of data gathered by Google about your site and usage
  • Google may be down or blocked (uniquely but can happen, i.e. some crazy corporate policy)
  • 1 new connection the user browser has to make
  • in Safari the jquery file is not served gzipped

In my experience, I got excellent results in hosting locally because I unified Google's JQuery lib with my other javascript code, gzipped it and got great compression rates in the unified javascript file. This way, the browser uses an already opened connection to download a "tiny" file with everything on it.

sopppas
Cons 4 and 5 are not specific to a CDN at all. And the point about parallelism is a non-issue: ordered script evaluation is handled by the browser regardless of using Google's CDN or hosting it yourself.
Crescent Fresh
ok, edited out the parallelism. Con 4 is about user performance, and con 5 is specific to Google's CDN, which is the question.
sopppas
Unless you are compiling your JS into one file, "1 new connection the user browser has to make" is an issue regardless of where you server it from.
Justin Johnson
Just comparing "serving JQuery locally" to "hosting it externally". My assumptions are based on results shown in Yahoo's YSlow and Google's Page Speed. In my experience, having it served locally scored me more points on those 2 profiling applications.
sopppas
A: 

Why only Google? Microsoft has launched a CDN too and these days I am linking to the MS CDN instead of Google to fetch Jquery.

If I rephrased your question to read 'What is the advantage of linking from a CDN?', I would answer it thus.

  1. The CDN will have a number of servers and there might be one closely geo-located and better serviced than yours (faster), though it won't matter cause typically JQuery will be a small fragment of your data.

That's it. I can't think of any other advantage. None. Nada. Nil.

In fact I can think of a disadvantage that you are going to have slightly less control over your content.

Don't think that the big corps are being generous with their money giving you free bandwidth. Being in control of your website is their business.

Cyril Gupta
Thanks for the mention of Microsoft's new CDN; didn't know about that.
Andreas Grech
Why MS over Google?
Justin Johnson
Why not?...........
Cyril Gupta
@Cyril Gupta - I think what Justin Johnson meant was, can you provide any reasons on why linking to Microsoft is better than linking to Google ?
Andreas Grech
@Justin - There's no special reason other than it exists just like Google's does and since it's there somebody ought to link to it. Unused things tend to whither and go away.
Cyril Gupta