views:

332

answers:

7

Whats faster for including scripts, using CDN (Google) or store them locally in website root?

+3  A: 

Depends on how fast your server is. The CDN however is probably faster, your client may have the file already cached from another website.

It'll offload a lot of bandwidth from your site, however you rely on the stability of a 3rd party to maintain the file. (Although I don't see google having issues any time soon)

Aren
+3  A: 

A Google CDN :-)
1) Optimized from cache point of view
2) User receives the resource from the more optimal CDN node

Claudio Redi
+13  A: 

If you mean the core jQuery libraries, use the google CDN for an internet-facing site (as opposed to an internal one).

The CDN has the following advantages you'll find hard to compete with:

  • More servers
  • The bandwidth (you don't pay for)
  • Geolocation (lower response time)
  • Redundancy
  • The optimized caching settings
  • Chance the user has already cached the file from there
  • Parallelized downloads, user can grab other content from your site at the same time

Though you can configure the cache headers just like they do, you probably can't serve the file faster. That being said, the library/CDN is only part of the puzzle. Miscellaneous plugins and code you have should also be minified, combined and served via gzip.

Nick Craver
Good point that use may already have the cached the file, not thought of that one before :)
Paul Groves
Awesome answer Nick, but what if CDN-served libraries are updated and suddenly it breaks the layout or similar?Isn't that a security breach?
purpler
@purpler - You have control there, for example you can reference a specific version which won't change, e.g. `http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js`, or the latest dot/minor version: `http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js`, or the latest major version: `http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js` (make sure to use `jquery.min.js` in production!)
Nick Craver
Bingo once again Nick, thanks for your answers, you made my day :)Just another thing, what is scripts like html5shiv are in question, it doesn't have the version numeration system:<!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
purpler
@purpler - For those scripts you'll need to include them from your server, make sure to have the CDN link for the library in first, just as you would locally. The jQuery team is working on a CDN with Media Temple (who currently host the jQuery site) to build a plugin CDN. I couldn't find any good online references to this, but if you listen to the jQuery podcasts or any of their meetings, this is brought up...soon as that's live, there should be a CDN option for most plugins as well.
Nick Craver
A: 

I'd say Google's CDN for reasons others have stated.

However, if your target market is in close proximity to your server it may be better to server it from your server.

Say, for instance, you have a site forOrlandoFloridaPeopleOnly.com/. If your server is hosted in Orlando, Florida and Google's closest content delivery servers are in Miami, Florida and Atlanta, Georgia (which is true), your server may [will probably] be faster if the visitor didn't already have a cached copy of the file from Google CDN.

Remember, if you do serve static content to your visitors form your server try to parallelize the downloads by utilizing sub-domains or other means. And for goodness sakes...don't transfer cookies for static content.

I'm not sure how reliable this source is: Google data centers. So don't necessarily count on it.

David Murdoch
A: 

It may be worth noting that Visual Studio has problems with Intellisense for Google's CDN. Microsoft also has a CDN that allows Intellisense to function correctly. But yes, use a CDN.

Aaron
A: 

Unfortunately studies have recently shown that Googles CDN actually hinders performance.

Google's AJAX Libraries API tried to uses network effects to improve performance of all participating websites by providing a common shared cache. However recent research has discovered that too few people use the network for it to hit critical mass and actually improve web performance. Currently the overhead in using the network means using Google's AJAX Libraries API actually lowers performance. You should host the JavaScript file locally. This will increase your bandwdith consumption but improve page load speed. From Zoompf.com performance report note.

See here also Should You Use JavaScript Library CDNs?

Duncan
can I get some feedback on why this is negative please?
Duncan
+2  A: 

You absolutely should use Google's CDN to host jQuery for you. Without rewriting a bunch of stuff check out these 2 posts about it. It is pretty convincing for why you should use it.

http://www.how-to-asp.net/caching-jquery-using-googles-cdn.aspx

http://encosia.com/2010/09/15/6953-reasons-why-i-still-let-google-host-jquery-for-me/

damstr8