Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundling .js files vs CDN

In order to improve performance of our web pages, we are recommended to use CDNs to serve .js files on our web pages. That makes sense.

Also, we are recommended to bundle our .js files in order to reduce the number of requests which are being made to server on load.

So, we need to sit down and make decision between if we use CDN or bundle .js files.

What are the pros and cons? Which ones make more sense?

like image 369
tugberk Avatar asked Sep 16 '11 14:09

tugberk


People also ask

Why do we bundle js files?

JavaScript bundling is an optimization technique you can use to reduce the number of server requests for JavaScript files. Bundling accomplishes this by merging multiple JavaScript files together into one file to reduce the number of page requests.

How do I bundle JavaScript files?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script. But that's just the bare minimum it can do.

Is it better to download or use CDN?

Because of its very core and design, CDNs can deliver content much speedier to any user and on any device. Whether a tablet or a laptop, your web content gets to each of your users much quickly. This can mean more money for your business! This is perhaps one of the best advantages a website owner gets from CDNs.

Is it good practice to use CDN?

It is still beneficial to use a CDN, and it is still for your users' benefit, but the benefit is in reduced latency with geo-located servers, not in general performance due to a hot cache hit.


2 Answers

Why can't you bundle them and place them are the CDN? It should hardly be a decision of one or the other?

If you have to choose one or the other, it depends on how many .js files you are including. For a small number of files, I'd suggest that a CDN would be quicker, where-as for a greater number of files, a bundle of .js files would definitely be quicker. Where the switch-over would be, is something for you to experiment with.

like image 170
Matt Avatar answered Oct 17 '22 00:10

Matt


My answer: both. Bundle them and place them on a CDN.

The downside of doing this? Depends. What does you build process look like? Can you easily automate the bundling and minification? Are you using Yahoo YUI or Google Closure or something else?

Also, if there is a lot of GUI dependent jQuery there might be some time consuming friction due to constantly changing elements/effects/css.

Testing is important too because due to possible minification quirks.

Bottom line: 5 javascript files safely bundled into 1 file === 4 fewer requests.

A page with just plain old Html and one external javascript reference === 2 requests to your server. However, a page with just plain old Html and one external javascript reference on a CDN === 1 request to your server.

Currently we are using the Google Closure tools. The Google Closure Inspector helps with the following:

Closure Compiler modifies your original JavaScript code and produces code that's smaller and more efficient than the original, but harder to read and debug. Closure Inspector helps by providing a source mapping feature, which identifies the line of original source code that corresponds to the compiled code.

like image 38
Kris Krause Avatar answered Oct 16 '22 22:10

Kris Krause