多个JavaScript文件,合并成一个 [英] Multiple JavaScript files, combine into one

查看:218
本文介绍了多个JavaScript文件,合并成一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC开发和使用多个JavaScript文件。例如。 jQuery的,jQuery用户界面,谷歌地图,我自己的JavaScript文件等。

I am developing in ASP.NET MVC and using multiple JavaScript files. E.g. jQuery, jQuery UI, Google Maps, my own JavaScript files, etc.

有关性能,我应该将其组合到一个?如果是这样,怎么样?

For performance, should I combine these into one? If so, how?

推荐答案

您想将多个文件合并成一个原因是如此,以尽量减少建立和拆除http请求的延迟,越少你做越好。然而,许多新的浏览器正在下载并行(仍在执行顺序)的JavaScript文件。其结果是,在下载一个1Mb的文件可能比慢3 350KB的文件。我特地做的是我的文件分成三捆:

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. However, many newer browsers are downloading JavaScript files in parallel (still executing sequentially). The consequence is that downloading a single 1Mb file may be slower than three 350Kb files. What I've come to do is to separate my files into three bundles:


  • 外部LIB文件(jQuery的,海军报,插件)

  • 内部LIB文件(由多个页面共享)

  • 页特定文件(仅由该网页由两个页面中,也许)

这样,我得到两全其美:没有过多的在启动时的HTTP请求,还,这并不是说不能从并行下载中获益的单个文件

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

这同样适用于CSS,每个页面放置三个CSS束。因此,在总,我们的网页下载6捆绑文件,以及主要的HTML文件。我觉得这是一个很好的妥协。您可能会发现文件的不同分组为你工作好,但我的建议是不要创建一个单一的捆绑,除非它是一个网页应用程序。如果你发现自己放在同一个文件分成不同的包了很多,现在是时候重新思考的捆绑策略,因为你同样的内容多次下载。

The same applies to CSS, each page load three CSS bundles. So in total, our pages download six bundled files plus the main html file. 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.

有什么用?马亭的建议是在钱上。 YUI是从我的经验是最广泛的应用,这是我们在我的previous和目前的工作中。

What to use? Martijn's suggestions are on the money. YUI is the most widely used from my experience, that's what we used at my previous and current jobs.

这篇关于多个JavaScript文件,合并成一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆