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

标签 javascript cdn

<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 引用更好,原因很简单,HTTP/2

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

来自他们题为 "HTTP/2 for Web Developers" 的博文,名为 "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,我们都需要开始质疑这些提高性能的标准技巧.

关于javascript - 许多 CDNjs 与一个缩小的本地 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38803203/

相关文章:

javascript - jQuery click() 在 "for"内循环触发一次

javascript - 向 Highcharts X 轴添加更多数据

javascript - 使用 HMAC 方法和 Google Apps 脚本生成带 key 的哈希值

jquery - CDN 上 jquery.validate 的可用翻译

performance - 使用 Cache 还是 CDN 更好?

javascript - 通过点击事件移动图像

javascript - 如何在 jquery countTo 中使用 onComplete() 函数

Azure Blob 存储及其 CDN 性能?

javascript - 通过 SSL 从 Google/Microsoft CDN 获取 javascript 库

Amazon CloudFront CNAME