简体   繁体   English

多个JavaScript文件合并为一个

[英]Multiple JavaScript files, combine into one

I am developing in ASP.NET MVC and using multiple JavaScript files. 我正在ASP.NET MVC中开发并使用多个JavaScript文件。 Eg jQuery, jQuery UI, Google Maps, my own JavaScript files, etc. 例如jQuery,jQuery UI,Google Maps,我自己的JavaScript文件等。

For performance, should I combine these into one? 为了表现,我应该将它们合二为一吗? If so, how? 如果是这样,怎么样?

The reason you want to combine many files into one is so to minimize latency of setting up and tearing down http requests, the fewer you make the better. 您希望将多个文件合并为一个的原因是为了最大限度地减少设置和删除http请求的延迟,您做得越多越好。 However, many newer browsers are downloading JavaScript files in parallel (still executing sequentially). 但是,许多较新的浏览器并行下载JavaScript文件(仍按顺序执行)。 The consequence is that downloading a single 1Mb file may be slower than three 350Kb files. 结果是下载单个1Mb文件可能比三个350Kb文件慢。 What I've come to do is to separate my files into three bundles: 我要做的是将我的文件分成三个包:

  • External lib files (jquery, flot, plugins) 外部lib文件(jquery,flot,plugins)
  • Internal lib files (shared by multiple pages) 内部lib文件(由多个页面共享)
  • Page specific files (used only by that page, maybe by two pages) 页面特定文件(仅由该页面使用,可能由两页组成)

This way, I get the best of both worlds: not an excessive number of http requests at startup, but also, it's not a single file that can't benefit from parallel downloads 通过这种方式,我得到了两全其美:在启动时没有过多的http请求,而且,并不是单个文件无法从并行下载中受益

The same applies to CSS, each page load three CSS bundles. 这同样适用于CSS,每个页面加载三个CSS包。 So in total, our pages download six bundled files plus the main html file. 总的来说,我们的页面下载了六个捆绑文件和主html文件。 I find this to be a good compromise. 我发现这是一个很好的妥协。 You may find that a different grouping of files works better for you, but my advice is don't create a single bundle, unless it's a one page app. 您可能会发现不同的文件分组对您来说效果更好,但我的建议是不要创建单个捆绑包,除非它是一个单页应用程序。 if you find yourself putting the same file into different bundles a lot, it's time to re-think the bundling strategy since you're downloading the same content multiple times. 如果您发现自己将相同的文件放入不同的捆绑包中,那么就应该重新考虑捆绑策略,因为您多次下载相同的内容。

What to use? 用什么? Martijn's suggestions are on the money. Martijn的建议是钱。 YUI is the most widely used from my experience, that's what we used at my previous and current jobs. 根据我的经验,YUI是最广泛使用的,这就是我在之前和当前工作中使用的。

For the question of whether you should, check out the link in Shoban's comment. 关于您是否应该问的问题,请查看Shoban评论中的链接。

For the question of how: 关于如何:

If they are all going to be included on all of your pages, then it doesn't really make a difference. 如果它们都将被包含在您的所有页面中,那么它并没有真正有所作为。 But if some are only relevant to certain pages, it would technically be better to keep them separated and only include the relevant ones on relevant pages. 但是,如果某些页面仅与某些页面相关,那么在技术上最好将它们分开并仅在相关页面上包含相关页面。

As far as I know, you should indeed : less files means less http get, hence better performance for the user when they first load the page. 据我所知,你应该确实:更少的文件意味着更少的http get,因此当用户第一次加载页面时,性能会更好。 So they will save a split second they will come on your page for the first page. 因此,他们将在第一页的页面上保存一瞬间。 But after, these files are cashed, and it makes then no difference at all... 但是之后,这些文件被兑现,然后它就没有任何区别......

I haven't digged into the javascript engines itselves, but a function in one file will be handled in the same way if it is in a big file or a small file. 我没有深入研究它自己的javascript引擎,但如果它在一个大文件或一个小文件中,一个文件中的函数将以相同的方式处理。 So it makes no difference in the execution. 所以它在执行方面没有区别。

So, save your time, don't change anything as it'll cost you too much time for too little reward, especially when you'll discover that you want the latest version of jquery (a new version came out today btw), and that you have to re-concatene everything... 所以,节省你的时间, 不要改变任何东西,因为它会花费你太多的时间来获得太少的奖励,特别是当你发现你想要最新版本的jquery时(今天出现了新的版本),以及你必须重新连接一切......

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

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