简体   繁体   English

连接所有JavaScript文件(包括供应商)是否有任何缺点

[英]Are there any disadvantages to concatenating all JavaScript files (including vendor)

To minimize the number of requests made to my server and improve load time, I was thinking about concatenating all my JS Files, including vendor js files like angular, jquery-ui, ... It's a common practice to do this, but I often see websites that don't include their vendor JS files in the same JS file. 为了最大限度地减少对我的服务器发出的请求数量并缩短加载时间,我考虑连接所有的JS文件,包括像angular,jquery-ui这样的供应商js文件......这是常见的做法,但我经常这样做查看在同一JS文件中不包含其供应商JS文件的网站。 I know that lots of sites use CDNs to reuse a cached version of the JS file that an other page might have used. 我知道很多站点都使用CDN来重用其他页面可能使用过的JS文件的缓存版本。 Howewer, sometimes, they are separate vendor JS files that are served on the same server, for example on Github : 但是,有时,它们是在同一服务器上提供的独立供应商JS文件,例如在Github上:

Github上

Github has : Github有:

  • Frameworks.js that contains Modernizr, jQuery 包含Modernizr,jQuery的Frameworks.js
  • github-...js that contains the JS from Github itself. github -... js包含来自Github本身的JS。

Is there a particular reason to do this ? 有没有特别的理由这样做? Are they any issues that could appear because you concat all your JS Files ? 它们是否会出现因为您连接所有JS文件而出现的问题?

The main reason is to minimise the number of http requests. 主要原因是最小化http请求的数量。 In a bigger project if you are using some pattern like MV* you might want to abstract each module in an external .js file. 在一个更大的项目中,如果你使用像MV *这样的模式,你可能想要在外部.js文件中抽象每个模块。 This can result in many files 30-40 for example. 例如,这可能导致许多文件30-40。 In that case you might want to concat then into single file. 在这种情况下,您可能希望将其连接到单个文件中。

In a smaller project where you are going to have 3-4 js file, you use then without concatenation. 在一个较小的项目中,您将拥有3-4个js文件,然后使用没有连接。 I use requirejs to define modules as AMD and have each module in external file. 我使用requirejs将模块定义为AMD,并将每个模块都放在外部文件中。 Then I use requirejs build script to manage dependency and concatenated deployment file. 然后我使用requirejs构建脚本来管理依赖关系和连接的部署文件。

Good tools to automate this process 自动化此过程的好工具

Depends on how your applicatio is. 取决于您的应用程序。 Hypothetically, say if you have 20 js files of 0.5MB each. 假设,如果你有20个js文件,每个0.5MB。 Out of which only 2 (1MB) are required on the first page. 其中第一页只需要2(1MB)。 You might want to keep them separate and combine others. 您可能希望将它们分开并与其他组合。 Load the others in deferred mode while on the first page. 在第一页上加载其他延迟模式。 So, combining files is good when you understand what all to combine. 因此,当您了解所有要组合的内容时,组合文件是很好的。 As a general rule, if you have very few files, then it really does not make a very big difference. 作为一般规则,如果你的文件很少,那么它确实没有太大的区别。

some of the things that you should think about before combining javascript files, 在组合javascript文件之前你应该考虑的一些事情,

when you combine 100 files together. 将100个文件组合在一起时。 you are cutting down on 99 requests. 你减少了99个请求。 But you are also doing the following: 但是你也在做以下事情:
* increasing the time it takes to download that single file (and may be blocking loading of an important page) *增加下载单个文件所需的时间(并且可能阻止加载重要页面)
* account for the time spent in parsing of the additional JS code that we just pulled from the server, when it might not even be required at this point. *考虑解析我们刚刚从服务器中提取的其他JS代码所花费的时间,此时甚至可能不需要。 The javascript engines are getting faster and faster, but it is still true, that a significant amount of time is spent in parsing of the javascript code. javascript引擎变得越来越快,但它仍然是正确的,花费了大量时间来解析javascript代码。

so, in short the answer is - "it is always a trade-off game and knowing your code well helps" :) 所以,简而言之,答案是 - “它总是一个权衡游戏,并且知道你的代码很有帮助”:)

http://tomdale.net/2012/01/amd-is-not-the-answer/ is an interesting read on similar lines. http://tomdale.net/2012/01/amd-is-not-the-answer/是类似行的有趣读物。

Performance wise it isn't going to make a difference. 表现明智,它不会有所作为。 If you're annoyed by having a separate file for all of your frameworks, then I guess you could do it. 如果你为所有框架都有一个单独的文件而烦恼,那么我想你可以做到。 Otherwise you're just going to fill a single file and potentially have issues when trying to update frameworks later. 否则,您只是要填充单个文件,并且在稍后尝试更新框架时可能会遇到问题。

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

相关问题 使用grunt安全地串联供应商javascript文件-使用严格 - safely concatenating vendor javascript files using grunt - use strict 使用grunt连接所有供应商的javascript文件? - Using grunt to concatenate all vendor javascript files? JSPM-使用import和使用script标签来包含客户端库文件是否有优点/缺点? - JSPM - Are there any advantages/disadvantages in including client side library files using import vs using the script tag? 动态包含CSS / JS文件的缺点? - Disadvantages of dynamically including CSS/JS files? 页面内的Javascript有什么缺点吗? - Any disadvantages to Javascript inside the page? 在将所有 Javascript 文件发送给客户端之前将其连接成一个有什么好处? - What are the benefits of concatenating all Javascript files into one before sending it to client? 使用长时间间隔的javascript setInerval有什么缺点吗? - Are there any disadvantages to using javascript setInerval with a long interval? Bower和GruntJS。 将供应商JavaScript连接到一个文件中。 - Bower and GruntJS. Concatenating vendor JavaScript into one file. 使用Prepros连接JavaScript文件失败 - Concatenating JavaScript files with Prepros fails 使用Ant连接JavaScript文件 - Concatenating JavaScript files using Ant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM