简体   繁体   English

许多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>

vs VS

<script src="/myMin.js">

(myMin.js will contain all js file concatenated together and minified) What is the best for performance? (myMin.js将包含连接在一起并缩小的所有js文件)性能最佳? I am using cdnjs because it solves the problem of people in other region downloading the js file directly from my server. 我正在使用cdnjs,因为它解决了其他地区的人直接从我的服务器下载js文件的问题。 For example, people in Asia don't have to download my js file from USA server, which is a huge performance issue. 例如,亚洲人不必从美国服务器下载我的js文件,这是一个巨大的性能问题。 cdnjs helps me with scattering js file all over the globe. cdnjs帮助我在全球范围内散布js文件。 Since cdnjs is downloaded asynchronously so when will myMin.js be preferred? 由于cdnjs是异步下载的,所以何时首选myMin.js

I would say that using lots of CDN references is preferable, for the simple reason of HTTP/2 . 我想说,由于HTTP / 2的简单原因,使用大量CDN引用是可取的。

Cloudflare supports HTTP/2, I believe, so you are better off by having multiple, single purpose scripts than one combined one. 我相信Cloudflare支持HTTP / 2,因此您可以通过拥有多个单一用途脚本而不是一个组合脚本来获得更好的效果。

From their blog post entitled "HTTP/2 for Web Developers" , the section called "Stop Concatenating Files" is particularly relevant: 在他们的博客文章“HTTP / 2 for Web Developers”中 ,名为“Stop Concatenating Files”的部分特别相关:

However, concatenating files is no longer a best practice in HTTP/2. 但是,连接文件不再是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. 即使只改变了一行CSS,浏览器也会被迫重新加载所有的CSS声明。

In addition, not every page in your website uses all of the declarations or functions in your concatenated CSS or JavaScript files. 此外,并非您网站中的每个页面都使用连接的CSS或JavaScript文件中的所有声明或函数。 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. 虽然HTTP / 1.1中的请求开销使得这是一个值得的权衡,但它实际上可以减慢HTTP / 2中的首次绘制时间。

Instead of concatenating files, web developers should focus more on optimizing caching policy. Web开发人员应该更多地关注优化缓存策略,而不是连接文件。 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. 通过将经常更改的文件与很少更改的文件隔离开来,可以从CDN或用户的浏览器缓存中尽可能多地提供内容。

We all need to start questioning these standard tricks for improving performance now that HTTP/2 is pretty widely supported . 我们都需要开始质疑这些标准技巧,以便在HTTP / 2得到广泛支持的情况下提高性能。

There are several things to consider to make a decision. 做出决定有几个方面需要考虑。 A couple of them: 其中几个:

Performance: Using a CDN is recommended as those .js files are heavily cached world wide. 性能:建议使用CDN,因为这些.js文件在全球范围内都是高度缓存的。 This will help the most with users that are located far away of your origin server location. 对于位于远离原始服务器位置的用户,这将最有帮助。 This also will help you save bandwidth usage in your origin, same as CPU and other resources. 这也将帮助您节省源中的带宽使用,与CPU和其他资源相同。

Framework compatibility: The disadvantage of using CDN files is that if the public library has any changes on a version update, your applications may stop working as some methods or features may change from version to version. 框架兼容性:使用CDN文件的缺点是,如果公共库对版本更新有任何更改,您的应用程序可能会停止工作,因为某些方法或功能可能会随版本而变化。 If you use a local file, you are sure that your application will work regardless of any version updates done to the public library. 如果您使用本地文件,则无论对公共库执行任何版本更新,您都确信应用程序将正常运行。

All in all, if you do not have many applications to support where a change in a method in one of the libraries would cost you a lot of changing to do, use the CDN versions of the libraries as they would ultimately save you resources; 总而言之,如果您没有很多应用程序来支持其中一个库中的方法更改会导致您需要进行大量更改,那么请使用库的CDN版本,因为它们最终会为您节省资源; let that be money or server resources. 让它成为金钱或服务器资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM