187

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.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
Xaisoft
  • 45,655
  • 87
  • 279
  • 432

17 Answers17

152

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 did not host jQuery-UI, since 2016 they do!!, 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.

juFo
  • 17,849
  • 10
  • 105
  • 142
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 26
    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 Oct 11 '09 at 11:02
  • 16
    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 Oct 11 '09 at 12:13
  • 5
    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 Oct 11 '09 at 13:08
  • 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 Oct 11 '09 at 15:34
  • 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 Aug 05 '10 at 21:56
  • 19
    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 Oct 06 '10 at 07:04
  • 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. – Ruan Mendes Oct 30 '10 at 00:00
  • @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 Oct 30 '10 at 00:05
  • 2
    @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 – Ruan Mendes Oct 30 '10 at 00:19
  • 1
    Here is another reason not to use CDN for intranet-only apps based on personal experience. Most likely your company allows outside access via a firewall/proxy server, so if a firewall/proxy server has issues (which does not happen often, but does happen) then your app will break. If you have local (intranet) CDN or deploy jQuery as part of your web app, your app will work even with firewall/proxy servers not working. – Alek Davis Nov 19 '10 at 01:23
  • Google also don't host jquery vsdoc files either. – Drew Freyling May 03 '11 at 19:57
  • 3
    @Nick Microsoft has moved it's CDN from ajax.microsoft.com to ajax.aspnetcdn.com. So there's no chance of blocking Microsoft's CDN as a part of blocking Windows Updates. – Sachin Joseph Aug 08 '13 at 10:22
  • 1
    Is there a point where SO threads should be closed or marked for an update? I just got here through Google and this information is (naturally) horribly out of date and not as useful as it was back in '09 (!) – Nathan Hornby Mar 06 '14 at 16:09
89

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
  • 59,815
  • 13
  • 117
  • 134
  • 7
    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 Aug 19 '10 at 05:01
  • 20
    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 Aug 22 '10 at 15:31
  • @DaveWard, can you verify this is still the case, or have the tables turned somewhat over the last few years? – snumpy Apr 26 '13 at 16:41
  • 3
    @snumpy: The Google CDN has stretched its lead quite a bit from what I've seen. There's nothing *wrong* with the Microsoft CDN. It's fast and has a few files that the Google one doesn't. The cross-site caching benefit is dependent on net-wide coverage though, and Google's dominates all others in that regard. – Dave Ward Apr 26 '13 at 17:35
  • Because I've switched from jQuery CDN to Microeoft's for hosting jQuery Mobile, I moved my other jQuery downloads to it from Google to reduce the number of DNS roundtrips. Just another factor :) – Rob Grant Jan 07 '14 at 14:43
  • Yet https://hostingcrown.com/jquery-cdn list Microsoft as No.1 having the fastest response time... – Dan Diplo Dec 19 '19 at 14:01
20

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
  • 1,796
  • 1
  • 13
  • 16
18

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

Depending on your needs, this may be relevant.

dp.
  • 8,138
  • 7
  • 33
  • 28
  • 23
    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 Oct 06 '10 at 07:05
15

It should also be noted that as ajax.microsoft.com is a sub domain of microsoft.com requests send all microsoft.com cookies adding to the overall time it takes to get the file back.

Also, ajax.microsoft.com is using default IIS7 compression which is inferior to the standard compression that other web servers use.

http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js - 33.4K

http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js - 26.5K

Also, as others have mentioned google CDN is way more popular which greatly increases the chance of a file being cached.

So I strongly recommend using google.

Alistair
  • 1,939
  • 2
  • 22
  • 31
  • 3
    This was a good objection at the time, but no longer applies as the recommended CDN domain name is now ajax.aspnetcdn.com. The blocking of *.microsoft.com objection also no longer applies. – Stephen Kennedy Apr 13 '11 at 13:56
  • this is true. glad they finally fixed this part of it. Now I don't feel so bad about including jquery validate / cycle plugin from the ms cdn. – Alistair Jul 20 '11 at 07:59
  • Cookies thing also no longer applies because of switch to aspnetcdn. – Rob Grant Jan 07 '14 at 14:44
11

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
  • 2,893
  • 15
  • 16
7

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 Cade
  • 1,351
  • 2
  • 17
  • 28
  • 1
    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! – Walden Leverich Oct 15 '10 at 04:26
  • 2
    So, your IE6 users experience a slightly slow experience. Good trade-off if you ask me. IE6 is on the decline... even in Corporate intranets. – Armstrongest Nov 25 '10 at 17:58
6

Is one potentially faster than the other?

I was actually curious of this myself so I setup a jsbin test page using each of the following and then ran it through webpagetest.org's visual comparison tool. I tested:

  1. ajax.googleapis.com
  2. code.jquery.com
  3. ajax.aspnetcdn.com
  4. cdnjs.cloudflare.com

Who was fastest: code.jquery.com by 0.1 second in both tests

Who was slowest: ajax.aspnetcdn.com by 0.7 seconds in first test and ajax.googleapis.com by 1 second in second test

Here's the 1st test (each was tested 3 times):

Video: http://www.webpagetest.org/video/view.php?id=121019_16c5e25eff2937f63cc1714ed1eac814794e62b3

Reports: http://www.webpagetest.org/video/compare.php?tests=121019_D2_KF0,121019_9Q_KF1,121019_WW_KF2,121019_9K_KF3

Here's the 2nd test (another 3 each):

Video: http://www.webpagetest.org/video/view.php?id=121019_a7b351f706cad2c25664fee7ef349371f17c4e74

Reports: http://www.webpagetest.org/video/compare.php?tests=121019_MP_KJN,121019_S6_KJP,121019_V9_KJQ,121019_VY_KJR

Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
4

As stated by Pingdom:

When someone visits your site, if they have already visited another site that uses the same jQuery file on the same CDN, the file will have been cached and doesn’t need to be downloaded at all. It can’t get any faster than that.

This means that the most widely used CDN will have the odds on its side, which can pay off for your site.

A few observations on performance: Google’s CDN is consistently the slowest of the three both in North America and Europe. In Europe, Microsoft’s CDN is the fastest.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
mayank
  • 2,615
  • 2
  • 23
  • 18
3

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
  • 3,964
  • 5
  • 27
  • 29
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Fiona - myaccessible.website Jun 25 '14 at 15:17
  • 1
    To Fiona, it is my answer to the question. The question is "does it matter", my answer is "it depends on where his target audience is located", and I provided a site to test speed from different places around the world to let him decide which CDN to use. It is not a comment, it's an answer. – silent Jun 26 '14 at 20:57
3

One additional consideration - if your site is SSL and you need to support Android 2.1 (or earlier), the SSL certificate on the HTTPS version of the Microsoft CDN will crash those versions of the Android browser, per this issue: http://code.google.com/p/android/issues/detail?id=5001. It's not Microsoft's "fault", as the SSL certificate is technically valid and the defect is in Android's SSL implementation... but it will crash your site, nonetheless.

The SSL cert on Google's CDN does not fall afoul of this particular issue (relating to the certificate's "Certificate Subject Alt Name").

So, for SSL + Android 2.1 support, use the Google CDN.

Kent McNeill
  • 361
  • 3
  • 11
2

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
  • 12,184
  • 19
  • 69
  • 92
1

Also consider when using Google CDN that some times people make typos such as ajax.googelapis.com. This could potentially create a really nasty xss (cross site scripting) attack. I have actually tested this out by registering a googlapis.com typo and very quickly found myself serving requests for javascript, maps, css etc.

I emailed Google and asked them to register similar CDN typo URL's but have not heard back. This could be a real reason not to rely on CDN's because there are potentially dangerous attackers awaiting the typo requests and can easily serve back jquery etc with an xss payload.

Thank you

1

Depending which industry the application targets, you may not want to use a CDN managed by other organisations. It often raises issues regarding to compliance, privacy and confidentiality.

For example, when you include Google Analytics in a secure application, the browser still sends the current URL as the "referer" header. Any identifiers, say a session id or secret token may appear in their logs. For example, if a client IP of 192.0.2.5references https://healthsystem.example/condition/impotence, then well, you can infer information which is considered to be rather private.

Other cases include information of consequence, such as an account number, social security number or session information in the URL. That sort of data should never be in the URL as it can be used outside of the application.

While you may trust Google, Microsoft or Yahoo, your users may not.

For industries like Finance, Legal and Health Care, you may want to establish your own CDN with the help of a vendor (e.g. Akamai) with which you can sign a BAA.

bloudraak
  • 5,902
  • 5
  • 37
  • 52
1

I would advise that you base your usage on the general location of the users you're targeting.

If your site is targeted for general public, then using Google's CDN would be a good choice.

If your site is also targeted at China, then using Microsoft's CDN would be a better choice. I know from my experience, as Google's servers kept getting blocked by the Chinese government, rendering websites that uses them un-loadable.

*Note that you can of cos create region specific sites, e.g. cn.mysite.com to cater specifically for China, but if you're low on resources and time, its worth a consideration.

Full list of Microsoft CDN here. http://www.asp.net/ajaxlibrary/cdn.ashx

They have since renamed to ajax.aspnetcdn.com, which reduces the likelihood of blockage by firewall rules.

KnaveT
  • 115
  • 1
  • 10
1

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.

alex
  • 479,566
  • 201
  • 878
  • 984
Netsi1964
  • 3,244
  • 1
  • 27
  • 17
-3

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\"></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
  • 10,234
  • 12
  • 54
  • 81
  • 6
    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 Nov 25 '09 at 00:29
  • 1
    This works :S Re read the script - if it isn't defined, it writes this and loads? – Wil Nov 25 '09 at 01:04
  • 12
    I've never understood why people do " –  Jan 15 '10 at 13:36
  • 3
    "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 tags as executeable code rather than as a string to be written." – SeanJA Feb 10 '10 at 23:44
  • 2
    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 Oct 18 '10 at 06:47