tags:

views:

5859

answers:

13

Does it actually matter which CDN you use to link to your jquery file or any javascript file for that matter. Is one potentially faster than the other? What other factors could play a role in which cdn you decide to use? I know that Microsoft, Yahoo, and Google all have CDN's now.

+9  A: 

It probably doesn't matter, but you could validate this with some A/B testing. Send half of your traffic to one CDN, and half to the other, and set up some profiling to measure the response. I would think it more important to be able to switch easily in case one or the other had some serious unavailability issues.

lod3n
+19  A: 

Update based on comments:

Short version: It doesn't matter much, but it may depend on what they host. They all host different things: Google doesn't host jQuery.Validate, Microsoft doesn't host jQuery-UI, Microsoft offers their scripts that would otherwise be served via ScriptResource.axd and an easier integration (e.g. ScriptManager with ASP.Net 4.0).

Important Note: If you're building an intranet application, stay away from the CDN approach. It doesn't matter who's hosting it, unless you're on a very overloaded server internally, no CDN will give you more performance than local 100mb/1GB ethernet will. If you use a CDN for a strictly internal application you're hurting performance. Set your cache expiration headers correctly and ignore CDNs exist in the intranet-only scenario.

The chances of either being blocked seems to be about equal, almost zero. I have worked on contracts where this isn't true, but it seems to be an exception. Also, since the original posting of this answer, the context surrounding it has changed greatly, the Microsoft CDN has made a lot of progress.

The project I'm currently on uses both CDNs which works best for our solution. Several factors play into this. Users with an older browser are still probably making 2 simultaneous requests per domain as recommended by the HTTP specification. This isn't an issue for anyone running anything decently new that supports pipelining (every current browser), but based on another factor we're knocking out this limitation as well, at least as far as the javascript.

Google's CDN we're using for:

Microsoft's CDN we're using for:

Our server:

  • Combined.js?v=2.2.0.6190 (Major.Minor.Iteration.Changeset)

Since part of our build process is combining and minifying all custom javascript, we do this via a custom script manager that includes the release or debug (non-minified) versions of these scripts depending on the build. Since Google doesn't host the jQuery validation package, this can be a down-side. MVC is including/using this in their 2.0 release, so you could rely completely on Microsoft's CDN for all your needs, and all of it automatic via the ScriptManager.

The only other argument to be made would be DNS times, there is a cost to this in terms of page load speed. On Average: Simply because it's used more (it's been around longer) ajax.googleapis.com is likely to be returned by DNS sooner than ajax.microsoft.com, simply because the local DNS server was more likely to get a request for it (this is a first user in the area penalty). This is a very minor thing and should only be considered if performance is extremely important, down to the millisecond.
(Yes: I realize this point is contrary to my using both CDNs, but in our case the DNS time is far overshadowed by the wait time on the javascript/blocking that occurs)

Last, if you haven't looked at it, one of the best tools out there is Firebug, and some plug-ins for it: Page Speed and YSlow. If you use a CDN but your pages are requesting images every time because of no cache-headers, you're missing the low-hanging fruit. Firebug's Net panel can quickly give you a quick breakdown of your page load-time, and Page Speed/YSlow can offer some good suggestions to help.

Nick Craver
Less likely to be blocked? I'd love to know how you came up with that idea. The MS network isn't MS's anyway, it's akamai's who have been doing load balanced servers for a lot longer than google has, which makes a nonsense of the "better fall-over system" as well. Really, if you're going to make claims like this some evidence would be nice.
blowdart
Some companies, and I've worked for a few, block *.microsoft.com outright as part of their blocking of windows update. Is this correct? No, does it happen? Yes. Example: http://ajax.microsoft.com/...it falls under the *.microsoft.com block and not under the www exception, it's blocked when a company chooses to block anything but www.microsoft.com. I didn't say it's very likely, I said it's more likely, as I've never seen google blocked but have seen the reverse.
Nick Craver
And I've seen google blocked to stop gmail at government sites. But as it's so rare, I would hardly attempt to use it as a justification in this case.
blowdart
Rather ignorant methods of blocking windows update aren't so rare, especially at smaller companies who can't/won't afford the expertise to do it correctly (even if it is trivial)...which is the reason for noting it.
Nick Craver
I've had problems with Google's CDN being blocked by companies who only allow domains they've specifically approved. They allowed *.google.com and *.microsoft.com, and added my client's site, but googleapis.com was unknown, so it was blocked. Many of these companies are still clinging to IE6 (over 33% of this site's traffic... ugh!), so even if they understood the reason for the CDN, that wouldn't mean they'd allow it. To play it safe, we decided to just host jQuery from our server.
Matt
Since this was written, MS have added jQuery-UI to their CDN: http://www.asp.net/ajaxlibrary/cdn.ashx#Using_jQuery_UI_from_the_CDN_10
Will Dean
Another reason not to use cdn for intranet apps is the fact that your app now depends on an internet connection. Many clients of ours have machines that don't have internet access.
Juan Mendes
@Juan - while true in *some* cases it's not in *most*. If my app requires an internet connection (and most web-apps do) then it's not a factor really, since the page with the script tags wouldn't be loading either :)
Nick Craver
@Nick: This may be your experience, I've been writing enterprise web applications for 11 years now and they're usually run from a server within a company and they communicate only with the originating server. My comment was meant to warn developers not to add a dependency to apps that don't already depend on an internet connection
Juan Mendes
+2  A: 

I think it depends on where is your targeted audience. You can use alertra.com to check both CDN speed from many locations around the world.

silent
A: 

I would use both!

As the Google Jquery hosting has been around a lot longer, the chances are much higher that people will already have it cached compared to the Microsoft one, so I would have it first.

Personally, I would use something like this -

if (typeof jQuery == 'undefined') {  
    // jQuery is not loaded  

  document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"&gt;&lt;/scr" + "ipt>");
        }
} else {
    // jQuery is loaded
}

(Not sure this 100% works, but I was just going to write the idea and not example - This references the Google hosted Jquery and not the Microsoft one as I couldn't find the link)

Wil
jQuery will never be defined unless you bring it into your page. Caching the .js file doe snot make it available to all the browser pages by default!
Falkayn
This works :S Re read the script - if it isn't defined, it writes this and loads?
Wil
I've never understood why people do "<scr" + "ipt ..."
Charlie Somerville
"Depending on the browser, the amount of other preceding javascript, and how well-formed the overall code is, this is done to prevent the parser from interpreting the <script> and </script> tags as executeable code rather than as a string to be written."
SeanJA
The problem is that `jQuery` will never be defined *unless* you've actively loaded it. In your script the first branch will *always* get executed (unless you have another jQuery inclusion above), rendering the script superfluous.
jensgram
+29  A: 

You should absolutely use the Google CDN for jQuery (and this is coming from a Microsoft-centric developer).

It's simple statistics. Those who would consider using the MS CDN for jQuery will always be a minority. There are too many non-MS developers using jQuery who will use Google's and wouldn't consider using Microsoft's. Since one of the big wins with a public CDN is improved caching, splitting usage among multiple CDNs decreases the potential for that benefit.

Dave Ward
if we keep thinking that way then only bigger will get to breath. Don't just use google because it's google and assume everyone is with them(no doubt most are with them). But let best win, compare result and go with them.
mamu
It's not an assumption. Sites in the Alexa top 200,000 using Google's CDN outnumber Microsoft's over 100:1. In terms of popularity for caching, the only point in favor of the MS jQuery CDN is that Microsoft.com uses it, which gives it a lot of exposure from that one reference alone (but not as much as the thousands of top sites referencing Google's).
Dave Ward
+6  A: 

Google will send you a jQuery version minified with their own software, this version is 6kb lighter than the standard minified version served by MS. Go for Google.

Oscar Kilhed
+12  A: 

One minor thing to consider is that both companies offer slightly different "extra" libraries:

  • Microsoft is offering the JQuery validation library on their CDN, whereas Google is not (http://www.asp.net/ajaxlibrary/cdn.ashx)
  • Google is offering the JQuery UI library on their CDN, whereas Microsoft is not (http://code.google.com/apis/ajaxlibs/documentation/)

Depending on your needs, this may be relevant.

dp
Since this was written, MS have added jQuery-UI to their CDN: http://www.asp.net/ajaxlibrary/cdn.ashx#Using_jQuery_UI_from_the_CDN_10
Will Dean
A: 

Here we go with those CDNs... Google CDN is down and so was my website.

http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

Jesse
A: 

I know I'm chiming in a little late here, but here is the code that I've been using in production. I've never had issues with it, but your mileage may vary. Make sure you test it in your own environment.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>    
<script type="text/javascript">
    !window.jQuery && document.write('<script src="/scripts/jquery-1.4.2.min.js"><\/script>')
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
    !window.jQuery.ui && document.write('<script src="/scripts/jquery-ui-1.8.2.min.js"><\/script>')
</script> 
Jeremy
Unfortunately some browsers (IE6) won't delay the processing of that online script until after the src= script is loaded so this won't work as expected. Wish it would!
WaldenL
A: 

My answer is bit different than others, I will go with microsoft if you need jquery validator which almost everyone need if you are using jquery.

Microsoft CDN http connection is Keep-Alive which is big plus when you are requesting multiple items.

So if you need jquery validation then use Microsoft CDN, even if you need jquery ui use microsoft because google not not keeping keep-alive so every request are on it's own. so mixing in that way is plus. if you are using microsoft only for validator then you are doing seperate connection with google server for each request.

mamu
+3  A: 

It's about statistics: jquery.com loads jQuery from Google. And so does Twitter, Stackoverflow and many many others. So, there are pretty high possibilities that your website user already have it cached = no download at all.

Forget validator, bandwith and speed because this is the major benefit. Otherwise, any other CDN option will perform essentially at the same level.

achairapart
Yeah, but Twitter (according to Dave Ward's http://encosia.com/2010/09/15/6953-reasons-why-i-still-let-google-host-jquery-for-me/) uses jQuery 1.3.0 (in "old" Twitter) so they don't really matter... yet...
veggerby
Well, sites I did some time ago still uses jQuery 1.3.0, like the (old) Twitter. It always matters.
achairapart
+1  A: 

Hi, In the summery it says that microsoft is not offering UI, that is not correct (any more). It can be downloadloade at http://www.asp.net/ajaxlibrary/cdn.ashx.

/Sten Hougaard

Netsi1964