At the FlatironSchool, we are currently learning about JavaScript and jQuery. Since we aren’t yet building our own apps from scratch, we’re usually given a template.
I took a closer look at one of the templates and will explain the following two lines, which are typically found at the bottom of your index page:
1 2 |
|
Line 1 in our code requires jQery from Googles CDN.
Line 2 basically says “If you can’t reach Google load the local version”.
This got me thinking. Why do we even need a CDN if we have a local version any ways?
Well, as it turns out, there are a number of reasons why this can be important…
What is a content delivery network?
A Content Delivery Network refers to a large network of servers deployed across multiple networks in several data centers, often geographically distributed.
Content Delivery Networks are all over the web. You use them every day when downloading files from Dropbox, or watching Orange Is the New Black on Netflix.
But apart from big consumer applications, they also play an important role in our day-to-day applications.
Three reasons why you should use a CDN for your code:
1. Low latency
When using a professional content delivery provider, there is a much higher chance of one of their servers being geographically close to your user. This may not be the case when you host your website on your own server.
As a result, content will be loaded faster.
2. Higher chance of having a cache-match
Static files like the jQuery library are cachable. Google will try and store them for up to one year in your computer. The more companies use their CDN to deliver jQuery, the higher the chances are that your user already has jQuery in their cache and does not require to download it at all.
3. Increased parallelism
Browsers typically limit the number of connections that can be made to any given site. Using a CDN will allow your users to download more of your local content in parallel. This in turn will increase the speed at which your app is up and running.
Conclusion
You may have noticed that all the stated reasons help increasing the speed of your app. Well, speed these days is important and every milisecond counts.
People aren’t patient so you should try and not only keep your code as clean and fast as possible, but also choose best practices over where to host your files and libraries.