views:

149

answers:

6

Should I use a local copy of jquery, or should I link to a copy provided by Google or Microsoft? I'm primarily concerned about speed. I've heard that just pulling content from other domains can have performance advantages related to how browsers limit connections. In particular, has anyone benchmarked the speed and latency of Google vs. Microsoft vs. local?

Also, do I have to agree to any conditions or licenses to link from a third-party?

+3  A: 

I have been using Google's AJAX library hosting in production for several clients. Works like a charm, and is definitely the best way to go.

http://code.google.com/apis/ajaxlibs/

David Souther
+2  A: 

I would suggest loading jQuery from the CDN that jQuery provides itself:

http://code.jquery.com/jquery-1.4.2.min.js

You don't have to sign up for any accounts, the source will download from as close to the user as possible, and you don't have to worry about licensing.

Justin Niessner
Alot of times I've found the jQuery website itself to be slow, if their main site is using their CDN then I probably wouldn't trust it.
Klinky
Google's a lot better set up to handle large amounts of load, I'd imagine.
ceejayoz
@ceejayoz The jQuery CDN is actually hosted by Media Temple (mt) which should be able to handle the load just fine...since that's what the company is set up to do.
Justin Niessner
My experience with MediaTemple hosting has been a lot more negative than my experience with Google.
ceejayoz
+2  A: 

I would recommend always hosting your own local copy.

  • The server could go down.
  • The server could change version of the hosted file.
  • Your user's could arbitrarily create too much load on the hosted server which they may not be thrilled about.

I think its reasonable to use a hosted link when you are posting sample code you want to "work" without the user having to download jquery.

David
I'm pretty sure google has plenty of bandwidth to spare
Earlz
1. So could yours. 2. That's why Google allows you to link directly to major (i.e. "jQuery 1.3.x") and minor (i.e. "jQuery 1.3.2") version numbers. 3. Too much load for Google?
ceejayoz
Probably true if Google is the hosting provider. Personally, I would not trust a third party server with a core app dependency that is trivially hosted with the app itself. However, if it satisfies someone's need, I wont nitpick.
David
You can specify the library version to use, at least when using google
baloo
Also, swapping from Google to local hosting takes about 10 seconds if you have to do it urgently.
ceejayoz
@ceejayoz Sure, your server could go down. But if that happens, the page that requests the jQuery include won't get served, so it's a non-issue. And why introduce the extra complexity of swapping (however trivial)? Things should be as simple as possible.
David Lively
+3  A: 

Anytime you use an asset hosted by a third party you increase the number of possible points of failure in your application. You also risk potential bugs resulting from changes made to the asset (say, fixing a bug or updating to a new version) by the hosting party.

Page performance can potentially suffer due to latency differences between your site and the host. Network outages between the client and the host can cause your page to fail, as can internet filtering on the part of their ISP. For instance, using code hosted by Google will cause problems for anyone viewing your site from China.

It's better for security, performance, stability and version integrity to keep all of your assets in one place. Unless you're running a ridiculously high-traffic site, you shouldn't worry about distributing your content.

It's also worth noting that while jQuery isn't exactly a featherweight include, it's not obnoxiously large and, like any JavaScript includes, should be (but is not guaranteed to be) cached by the browser.

David Lively
Good point about clients from China.
Echo
This is happening a lot today. Whoever keeps voting -1: post a reason, please. Feedback voting is intended to improve the value of the answer archive.
David Lively
+7  A: 

One advantage would be that a user may already have it cached since another site also linked to a 3rd party.

I've been using google for this and haven't experienced any problems with it so far. You can easily load jQuery using:

<script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script type="text/javascript">
    google.load("jquery", "1.4");
</script>
baloo
If you're only going to use jQuery, you can include jQuery directly using the "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" URL, instead of both the JS API plus the jQuery code.
Alec
Yes, you should include it directly. Google's jsapi loader is only cached for one hour, so using it results in a lot more HTTP requests than necessary over time.
Dave Ward
+4  A: 

Most recommendations I have seen have been to use the hosted version of Google or Microsoft etc.

Dave Ward has a nice article explaining the reasons.

3-reasons-why-you-should-let-google-host-jquery-for-you

  1. Decreased Latency
  2. Increased parallelism
  3. Better caching

See his post for stats.

Dave does point out that you should only do this for Public Facing websites.

orandov