jQuery 一个大的 javascript 文件还是多个较小的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15236767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One big javascript file or multiple smaller files?
提问by Jason Silberman
Ok, so I have a reasonable size project, where I'm using jquery backbone and a couple of other javascript libraries. I was wondering if I should have one file for my javascript libraries and another for my custom code. Or a bunch of separate javascript files.
好的,所以我有一个合理大小的项目,我在其中使用 jquery 主干和其他几个 javascript 库。我想知道是否应该为我的 javascript 库创建一个文件,为我的自定义代码创建另一个文件。或者一堆单独的javascript文件。
回答by Niet the Dark Absol
It is generally a good idea to have fewer HTTP requests. So you should reduce the number of files as much as is reasonable.
减少 HTTP 请求通常是个好主意。所以,你应该尽可能减少文件的数量是合理的。
My personal preference is to have three "groups" of JavaScript files:
我个人的偏好是拥有三个“组”的 JavaScript 文件:
- Core file. Contains functions that are used almost everywhere and other useful page initialisation things.
- Module files. Contains code that is used in several places, but not everywhere. Can be dropped in to provide additional functionality. For instance, if you have a script to handle date inputs, you could include it as a module file and add it to pages that have date inputs.
- Page-specific files. These files contain code that is only used in one place. The only reason they're added as separate files than as part of the page itself is for cache reasons.
- 核心文件。包含几乎无处不在的函数和其他有用的页面初始化内容。
- 模块文件。包含在多个地方使用的代码,但并非无处不在。可以插入以提供附加功能。例如,如果您有一个处理日期输入的脚本,您可以将其作为模块文件包含并添加到具有日期输入的页面中。
- 特定于页面的文件。这些文件包含仅在一处使用的代码。它们作为单独的文件而不是作为页面本身的一部分添加的唯一原因是缓存的原因。
回答by Glenn Ferrie
One big file. You should minify the code when it goes to production and compress it if its large. You want to make as few requests to the server as possible to improve page performance
一个大文件。您应该在投入生产时缩小代码,并在代码较大时对其进行压缩。您希望向服务器发出尽可能少的请求以提高页面性能
回答by ZER0
One big file or two files: one small and one big. To be clear, during the development it's good have separate files – maybe using something like requireJS. But when you deploy it, it's good compress everything in one file, in order to reduce the HTTP latency and requests.
一个大文件或两个文件:一个小一个大。需要明确的是,在开发过程中最好有单独的文件——也许使用requireJS 之类的东西。但是当您部署它时,最好将所有内容压缩在一个文件中,以减少 HTTP 延迟和请求。
I mentioned two files. In some cases, it could be good having one small file, that takes care of the "bootstrap" operations, meanwhile the "big file" – especially if it's reallybig – is downloaded. This is useful especially for the first access, because users doesn't have your files cached yet.
我提到了两个文件。在某些情况下,有一个小文件可能会很好,它负责“引导”操作,同时下载“大文件”——特别是如果它真的很大——。这对于第一次访问尤其有用,因为用户还没有缓存您的文件。
回答by bchurchill
It's best to separate it out, but not get overzealous. That way you can reuse your library code later. Also, everyone likes working with separate files more because it keeps things more organized.
最好把它分开,但不要过分热情。这样你以后就可以重用你的库代码。此外,每个人都更喜欢使用单独的文件,因为它使事情更有条理。
That said, it's also best to give the user one compressed file so that everything can be cached easily, and this also reduces the number of page requests. Rails 3 does this automatically in the asset pipeline, for example. You can write a script to run your favorite compressor. But you shouldn't sacrifice code readability for this -- you can have your cake and eat it too!
也就是说,最好为用户提供一个压缩文件,以便可以轻松缓存所有内容,这也减少了页面请求的数量。例如,Rails 3 在资产管道中自动执行此操作。您可以编写脚本来运行您最喜欢的压缩器。但是你不应该为此牺牲代码可读性——你可以吃蛋糕也可以吃!
回答by Gerard Sexton
As suggested it is nice to work with smaller files, but for production code, your build process should include optimization. Part of that optimization should be minimizing the file sizes and network traffic optimzation, by combining into a single js file to reduce calls made by the browser.
正如建议的那样,使用较小的文件很好,但是对于生产代码,您的构建过程应该包括优化。该优化的一部分应该是最小化文件大小和网络流量优化,通过组合成单个 js 文件来减少浏览器的调用。
回答by iGanja
As a rule, I go with as few as possible simply to reduce the number of requests made to the server.
通常,我会尽可能少使用,只是为了减少向服务器发出的请求数量。
回答by Amy
Depends on the size of your application. But typically always better to group your javascript files appropriately for better maintainability and re-usability.
取决于您的应用程序的大小。但通常总是更好地将您的 javascript 文件适当地分组以获得更好的可维护性和可重用性。
You could use a JS module loader like RequireJSto load your JavaScript. At least the files will be organized. You can enhance server performance by making sure these files can be cached on the user's browsers so that they only download them once.
你可以使用像RequireJS这样的 JS 模块加载器来加载你的 JavaScript。至少文件将被组织。您可以确保这些文件可以缓存在用户的浏览器上,以便他们只下载一次,从而提高服务器性能。