I recently got a question from someone on our HeadJS Github page asking if they could link HeadJS on a CDN and apply a local fallback.
My first reaction was, of course you can, it's the same as for jQuery, and after all, using CDN's and the like are all good, right ? At least that's what everyone has been preaching to us these past few years.
Then, about 10 minutes later when the information finally sunk in and i started to think about the various experiences I've had in the past with different libraries and CDN's, i started to question my response...
My answer now to: Is using a CDN for a JS Framework really worth it?
Maybe, but probably not !
Important: If you host your sites static content on a CDN, you're good to go and not concerned by this. This explanation only applies to linking to various JS libraries out there
to a CDN, like jQuery, which will be my example.
The idea behind a CDN is that site A references a library on it, and if you visit site B, and site B also references it,
you have a hit, so the cache of the user will already be primed, and he won't have to download the resource again. You save bandwidth, and the users experience will be faster.
Everybody is happy!
Right ? Maybe..
So as the idea lays out, a CDN is good when as many sites as possible link to the same resource, and the user hits on those same resources as he travels from site A to B to C, etc..
If we look at W3techs, they say jQuery has a market-share of 56.7%, followed by Mootools with a 5.1% market-share.
And if we look at BuiltWith, we get something similar but broken down to popular site ranges.
Another chart from BuiltWith shows this layout
So what does this tell us ?
- A user encounters jQuery 1 site out of 2 (this is very good !)
- Forget about other libraries right away !
Ok, but what CDN should we use ..if any?
* jQuery's CDN does support HTTPS now
Judging from the above chart we can assume that the Official jQuery CDN is best appropriated. I would also give extra points here, since it's the default link on the jQuery site which gives it extra coverage.
That said, may people are also fans of Google's CDN, and some of Microsoft's ..which is fine, BUT.
As mentioned above, jQuery is used on almost 1 site out of 2, but which version, is it hosted on a CDN or locally, if on a CDN, which CDN...
Let's do some Math
The math that follows is totally theoretical and has no real world measures ..but should state a point.
Out of 100% of the web
50% uses jQuery
Let's say 25% of those 50% actually use a CDN
This leaves us with 12.5% of all sites on the web that use jQuery & a CDN
..now how many different versions of jQuery are there?
Looking at Google's CDN, we see the above chart. If we count only the 1.x branch we have 30 versions. Ouch !
How many sites do you know that actually use the latest and greatest and always keep their code up to date ? ..me, not many :-(
So let's consider that most sites are using 1 of 10 eventual versions instead of all 30.
Our 12.5% becomes a mere 1.2% now...
Almost forgot, there are many CDN's out there, thought the most popular are probably: jQuery CDN (for jQuery at least), Google, Microsoft.
Since we are still talking about jQuery let's say jQuery's CDN has a 70% market share, and the rest evenly split. Which now gives 0.84%, and 0.12%.
So ..using jQuery via a CDN, depending on which you use you have a 1 in 119, or 1 in 833 chance of the user having a cached copy.
Some other points to consider
If the CDN goes down or takes time to respond, it will make your whole site lag for the duration of the timeout (fallback scripts take at least 15secs to kick in ..you are using one right ?).
Sure, most CDN's have a SLA saying 99.9% up-time, but personally i have experienced first-hand that things are not always that great on jQuery's, Google's, or Microsoft's CDN.
When mainly serving users near your data-center (and up to a 1000km radius), chances are things will be slightly faster via your own data-center than via a CDN.
Update: I had some feedback talking about user browsing behavior and primed caches ..in a perfect world this would work as expected, but in reality
stats are as follows:
- ~30% of users have a full cache (capped at 320 MB)
- for users with a full cache, the median time to fill their cache is 4 hours of active browsing (20 hours of clock time)
- 7% of users clear their cache at least once per week
- 19% of users experience “fatal cache corruption” at least once per week thus clearing their cache
Source: Cache them if you can: S. Souders
- If you want to link to some JS Framework on a CDN ..if it's jQuery, sure why not.
- You don't have a lot to gain, but also not much to lose.
- If it's not jQuery ....your wasting your time.
- Don't use a CDN for simple caching purposes
If however you have many international users, chances are that the CDN will be faster (it's not about caching, but getting your content closer to your audience)!
In this case you should also start thinking (if you haven't already done so) of moving all your static content (js, css, fonts, images) to a real CDN to make your site faster.