许多 CDNjs 与一个缩小的本地 js

Many CDNjs vs one minified local js

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.1/jquery.inputmask.bundle.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/1.5.3/numeral.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/Ladda/1.0.0/spin.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/Ladda/1.0.0/ladda.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-modal/2.2.6/js/bootstrap-modalmanager.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-modal/2.2.6/js/bootstrap-modal.min.js"></script>

<script src="/myMin.js">

(myMin.js 将包含所有连接在一起并缩小的 js 文件) 什么是最好的性能?我用的是cdnjs,因为它解决了其他地区的人直接从我的服务器下载js文件的问题。例如,亚洲人不必从美国服务器下载我的 js 文件,这是一个巨大的性能问题。 cdnjs 帮助我将 js 文件分散到全球各地。由于 cdnjs 是异步下载的,所以什么时候 myMin.js 是首选?

做出决定时需要考虑几件事。其中几个:

性能: 建议使用 CDN,因为这些 .js 文件在世界范围内被大量缓存。这对远离源服务器位置的用户帮助最大。这也将帮助您节省源中的带宽使用量,与 CPU 和其他资源相同。

框架兼容性: 使用 CDN 文件的缺点是,如果 public 库在版本更新时有任何更改,您的应用程序可能会停止工作,因为某些方法或功能可能会因版本而异。如果您使用本地文件,无论对 public 库进行任何版本更新,您都可以确保您的应用程序能够正常工作。

总而言之,如果您没有很多应用程序来支持其中一个库中的方法的更改会花费您大量的更改,请使用这些库的 CDN 版本,因为它们最终会为您节省资源;让它成为金钱或服务器资源。

我会说使用大量的 CDN 引用更好,原因很简单 HTTP/2.

Cloudflare 支持 HTTP/2,我相信,因此拥有多个单一用途脚本比一个组合脚本更好。

来自他们的博客 post 标题为 "HTTP/2 for Web Developers", the section called "Stop Concatenating Files" 特别相关:

However, concatenating files is no longer a best practice in HTTP/2. While concatenation can still improve compression ratios, it forces expensive cache invalidations. Even if only a single line of CSS is changed, browsers are forced to reload all of your CSS declarations.

In addition, not every page in your website uses all of the declarations or functions in your concatenated CSS or JavaScript files. After it’s been cached, this isn’t a big deal, but it means that unnecessary bytes are being transferred, processed, and executed in order to render the first page a user visits. While the overhead of a request in HTTP/1.1 made this a worthwhile tradeoff, it can actually slow down time-to-first-paint in HTTP/2.

Instead of concatenating files, web developers should focus more on optimizing caching policy. By isolating files that change frequently from ones that rarely change, it’s possible to serve as much content as possible from your CDN or the user’s browser cache.

既然 HTTP/2 是 pretty widely supported,我们都需要开始质疑这些提高性能的标准技巧。