jQuery – Issues to Consider While Loading jQuery

googlejqueryperformancewebsite-performance

  1. I have seen many answers in SO regarding using Google's CDN to load jQuery instead of
    loading it from the local server. In essence I understand this is the issue related with decreased latency, better caching. But I want to know the technical answer, perhaps an example mentioning why it is even an issue to just load a 30KB file. We see nowadays websites with rich multimedia features which do significant download from server, so in such light does this file size make any difference?

  2. Also there are huge concerns regarding using such "heavy weight" library if all you are using is just a fraction of subset of features? And if you are going to use at all, it is recommended to use compressed version (30+ KB) instead of the uncompressed one(200+ KB)? I also know the usual answers to it, perhaps if someone can shed more insight to it?

My both questions mentioned above are essentially trying to raise the same issue. I would be very grateful for answers that can provide the perspective to it rather than usual answers in SO?

Best Answer

As you know, it's all about performance!

If you are work on a large project, which covers modern UI concepts, maybe you will have bunch of different JavaScript libraries hosted on your server. As you may know, browsers are limited to maximum of few (see 'connections per hostname' column) HTTP requests to specific host.

Let's say you have a site that needs to load a lot JavaScript libraries, and if they are minified, they are between 20-50KB each.

So, simply math: 10 libraries * 40KB = 400KB!

And it's very important to keep in mind that:

  • Access to particular resource (image, stylesheet, etc) is a separate HTTP request.
  • There are still people who do not have very good internet connection or they are connected to public WiFi networks that are used by many people.

So I think in this case using of CDN (not only for jQuery, and whenever is possible) make sense because:

  • Increase parallel downloading overcoming the limitation of browsers.

  • Since CDN is used by a lot of sites nowаdays, there is a really big chance to have already cached version of the resource.

  • It ensures that the user will get geographically close response.

And this point is valid both for your questions:

  • It reduces the amount of bandwidth used by your server (it may be a problem sometimes)

But there is something very important, which is ignored by many developers. They are using CDN to get latest version of a library, that is completely wrong! When new version is released, although in most cases, new functionality is added, there are cases when existing functionality can be removed (ex. function becomes deprecated) or modified!

So, it's a really good practice always to set a specific version of needed resource when you are using CDN.

Related Topic