3

I know this is not programming question but for knowledge I want to ask that

while using jQuery library file like jquery-1.8.0.min.js is it better to download and include in js folder

or use direct link like http://code.jquery.com/jquery-1.8.0.min.js

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
user2729017
  • 77
  • 1
  • 8
  • 1
    local file is better because you have access to jquery library when working offline. – MrNew May 25 '16 at 15:42
  • 1
    It really depends. A local file is going to generally be quicker and will give you the option of working in an unconnected / offline environment, however if you are going to be serving a large number of users, you might prefer to use a CDN like your second option to avoid additional overahead for your server. There are tons of different reasons (i.e. being in control of your own files, caching, etc.) – Rion Williams May 25 '16 at 15:43
  • @MrNew if my site is online – user2729017 May 25 '16 at 15:45
  • 1
    Possible duplicate of [Where do you include the jQuery library from? Google JSAPI? CDN?](http://stackoverflow.com/questions/547384/where-do-you-include-the-jquery-library-from-google-jsapi-cdn) – Noam Hacker May 25 '16 at 15:46
  • @RionWilliams why to use second option if my site will serve large number of user? In my opinion if I use second option then user have to request my site and my site have to request to `http://code.jquery.com` and download the js file. – user2729017 May 25 '16 at 15:49
  • Bandwidth constraints and server-overhead. This can obviously vary, but if you have a million users accessing your site daily and each of them are downloading an ~80k file, then that could quickly add up depending on how / if you pay for bandwith. Users might also see improved performance since CDNs are generally widely distributed geographically instead of just having the file on your own server. – Rion Williams May 25 '16 at 15:52

3 Answers3

3

It really depends.

Benefits of a Local File

  • You are in control of the file. You decide what is used, what version and any changes require you to explicitly change the file.
  • The file is already on your server. There is no need for an additional request to an external source that may / may not be available. This is important if your app may possible be accessed in an intranet or non-connected environment.

Local files will enable you to do things like "bundling" multiple of your own files and dependencies together and then optimizing them into a smaller single file. This is very useful if you have other things that depend on jQuery (like Bootstrap, etc.).

Benefits of a CDN

  • No Server Overhead or Bandwidth Constraints. Since you aren't serving the file yourself, you don't need to worry about how many users are going to be downloading it as it isn't coming from you at all.
  • Caching and Smarter Serving. CDNs generally take heavy advantage of caching, which can typically speed things up for users. CDNs also are geographically distributed and can handle serving users from around the globe as opposed to your server serving from a single location.

CDNs will really shine if you are going to be expecting a large amount of traffic to your site from a diverse distribution of users. They can also save you quite a bit if you actually have to pay for the amount of bandwidth you use as serving an ~80k jQuery file to 1 million users per day would add up, whereas a CDN would handle that burden for you.

Resources

You might consider reviewing over a few of the following resources to get a bit of insight from others that compare the pros and cons of each of these options to determine what might be best for you :

Community
  • 1
  • 1
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
2

Direct Link

PRO

  • Your site's performance might benefit a bit from caching.

CON

  • It is possible that the jQuery link could fail, or change. You have no control over this.

Downloaded

PRO

  • You control serving up the code, and it can't let you down outside your control

CON

  • Your users might experience a slight delay when downloading jQuery from your site.

Overall I'd say that the slight delay from hosting jQuery yourself is trivial for most modern internet speeds. But others may disagree.

Others may have more PROs and CONs to add..

Toby
  • 1,537
  • 10
  • 19
0

Short answer:

use cdn when

  • not working local
  • not using AMD (in some cases)

In most cases your server hasn't a better availability than google or cloudflare.

Tob
  • 627
  • 6
  • 9