5 reasons you should never locally host JavaScript libraries

JavaScript libraries should always be offloaded to a public hosting service.

I might or might not be a good frontend developer, but I care a lot about writing efficient and maintainable code. The first rule of being a “good” frontend dev is to learn all the libraries there are and ever will be. Nobody cares how you bake it, what you bake is all that matters. Who would write a 100 line JS script when you may just offload the 95 to a library? 🙂

If you are like me, you probably use lots and lots of JavaScript libraries. When I first started to use JavaScript libraries – I would simply download a library, put it in some folder, and link to it from my code. After two years of random JS coding, here why I now always use a JavaScript library hosting service (e.g. CloudFlare’s cdnjs.com or Google’s Hosted Library ) and NEVER locally host JS libraries:

1. Organized workflow

When you are working on a big project with several developers, it’s easy to write Spaghetti codes. You may not realize the impact a mere JS library creates on a codebase, but we are not talking about one libary here. When you get to work on a 5 years old codebase with 7 versions of JQuery, 3 versions of Leaflet and 2 versions of D3.js, you will know what I am talking about.

Offloading JavaScript libraries to a purposefully built public CDN helps you organize your workflow.

2. On-site cache

When you locally host your JavaScript libraries, it’s very easy to end up with 3 copies of one library file and use them for different pages. Your workflow may not have a data deduplication system, in which case your visitors’ browsers will have to load one file three times. If you have a server-side caching system in place (e.g. Varnish, redis), it will reduce the efficiency of that system.

3. Off-site cache

Public CDNs are everywhere these days. From Facebook to WordPress.com to NYTimes and Bloomberg, the hype is real!

Chances are, if you are using a popular library – your visitor’s browser already has a version of that cached from an earlier visit. If you use a hosting service for your libraries, your visitor browser will load that library from the disk cache. It will result in faster page load time, and most importantly – a happier userbase. 🙂

4. Speed

JavaScript library hosting services are designed to be fast.

cdnjs.com, one of the big 3 hosting services – is maintained by CloudFlare. With customers like the FBI and Reddit, CloudFlare claims to be the “largest web performance” company. CloudFlare controls literally half the internet, operates the largest CDN network and DNS service. The other two are Google’s Hosted Libraries and Microsoft JavaScript Runtime Hosting. I can’t speak for Microsoft, but Google is the big brother.

5. Bandwidth

Offloading your JavaScript libraries can save your server some serious bandwidth!

JavaScript libraries are usually very small. Some are larger, but let’s not talk about them. Most of the libaries are 30-100 kilobytes. Comparing to the other resources, it may not look that big on paper.

A busy website (aka “viral” sites) often receives one million hits a day. When you multiply your library file size a million time, the number certainly gets bigger. If you want to save some bandwidth and CPU time, start using a JavaScript hosting service today!

Ashik Emon

Ashik Emon

Ashik Ishtiaque Emon is a Google-certified Web Performance Expert and LPI-recognized Linux System Administrator. He travels and reads a lot. Reach him at [email protected].

More posts you might like: