Is it better to use CDN for js and similar resources vs local?

The benefit of a CDN is not (and should not be) for you. It is for your customers / users. Using CDN files - especially for widely used libraries like jQuery (or any other big library) - means they will receive a cached copy of the file from a physical location near them. In most cases, using a popular CDN for big libraries means the user has also downloaded that resource before - from someone else's site - so the file will be cached locally in their browser.

This helps reduce the time your user spends downloading files. It is not about your server load or your development ability in all but the smallest margin of edge cases. Think about your users first!

Using a CDN for most static files is strictly better for your users.


Update 2022-01-25:

It's worth noting that while this was true for a long time, it's really not the case any more. There are still real benefits from using CDNs (for example: files are served from a location in close physical proximity to the consuming end-user - on large, globally distributed CDNs), but the cross-domain resource caching is no longer a benefit.

@x-yuri noted this near the end of 2021, and all the major browsers implemented cache partitioning no later than 2020.

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.


Every HTTP/HTTPS request is an effort for the web server. Requests made for JS, CSS or any other static file would be redirected to a CDN which usually has a distributed, fast and stable architecture.

If you are managing a web app or site with pages that perform many requests and you are receiving a lot of users, the number of HTTP requests per second may increase dramatically; if your web server starts to respond slowly it would be a good idea to split the requests, CDNs are really good at managing static contents.

Basically performing tuning, the server may be scaled less and save money.

If you are not facing a lack of performance maybe you don't need to use a CDN.

Tags:

Html

Php

Cdn