3

Is it better to reference jQuery file inclusion through jQuery CDN for better performance?

Like

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>

Or

Is it better to reference stored jQuery file in our project?

Like

<script type="text/javascript" src="/js/jQuery.min.js"></script>

As jQuery CDN link handles caching, Does it improve performance more than jQuery file included from our project?

Smile
  • 2,770
  • 4
  • 35
  • 57
  • 1
    Is better using CDN, but always have a fallback to local see: http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-go – Irvin Dominin Oct 03 '13 at 07:05
  • The only drawback is if google servers get unreachable .. As @IrvinDomininakaEdward said, consider using a fall-back too – Stphane Oct 03 '13 at 07:13

3 Answers3

7

Do like this with a fallback

<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/libs/js/jquery-3.2.1.js"><\/script>')</script>

CDN first, if not (or blocked) ? so bring it from your server

Why should I use Google's CDN for jQuery? (For me, my point is about Cache Hit)

Community
  • 1
  • 1
l2aelba
  • 21,591
  • 22
  • 102
  • 138
4

First variant. Because google has best world-wide servers.

  • Decreased Latency
  • Increased parallelism
  • Better caching

The best way to initiate jQuery engine:

<script src="http://www.google.com/jsapi"></script>
<script>
  google.load("jquery", "1.10.2");

  google.setOnLoadCallback(function() {
  // Place init code here instead of $(document).ready()
 });
</script>

Here is a good article about this:

http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/

PingOfDeath
  • 137
  • 5
1

With CDN :

  • If the user have already downloaded the file on an other website, the file is already in cache

  • If the user haven't the file, the file will always be located on the server nearest to your visitor

  • In addition, you will reduce the load on your server, which allows it to render the site faster

GG.
  • 21,083
  • 14
  • 84
  • 130