Javascript - 结合还是不结合,这就是问题
好的,所以我知道为了提高效率,将所有页面 Javascript 合并到一个外部文件中是显而易见的,但这并不是这里的问题。
假设我有一个带有搜索字段的 Default.htm,其中附加了一些 Javascript 魔法。然后我有一个带有联系表单的 Contact.htm,其中附加了一些 Javascript 魔法。最后,我有一个 FAQ.htm,其中有一些 jQuery 面板显示答案...您明白了。
基本上我有三个页面,它们都需要“一些”JavaScript,但任何其他页面上都没有使用任何 JavaScript。
是否最好将所有 Javascript 合并到一个大的缩小文件中,加载一次,然后存储在缓存中,或者最好在默认页面上使用单独的 Javascript 文件,但不在联系页面上使用它...... ETC?
在这种情况下什么最有效?
选项:1
默认.htm
jquery.js
默认.js联系方式.htm
jquery.js
联系.js常见问题解答.htm
jquery.js
常见问题解答.js
选项:2
默认.htm
jquery-default-contact-faq-min.js联系方式.htm
jquery-default-contact-faq-min.js常见问题解答.htm
jquery-default-contact-faq-min.js
PS:对于所有 ASP.NET 人员,我正在使用 Combres 来合并、缩小和版本化我的 Javascript 文件
Ok, so I know that it's obvious to combine all of a pages Javascript into a single external file for efficiency purposes, but that's not quite the question here.
Say I have Default.htm with a search field that has a little Javascript magic attached to it. Then I have Contact.htm with a contact form that has some Javascript magic attached to it. And finally I have a FAQ.htm with some jQuery panels showing the answers... you get the picture.
Basically I have three pages that all have "some" javascript need, but none of the Javascript is used on any other pages.
Is it better to combine all of that Javascript into one big minified file that loads once and is then stored in Cache, or is it better to use an individual Javascript file on the Default page, but not use it on the Contact page... etc?
What works best in this scenario?
Option: 1
Default.htm
jquery.js
default.jsContact.htm
jquery.js
contact.jsFaq.htm
jquery.js
faq.js
Option: 2
Default.htm
jquery-default-contact-faq-min.jsContact.htm
jquery-default-contact-faq-min.jsFaq.htm
jquery-default-contact-faq-min.js
PS: for all you asp.net guys, I'm using Combres to Combine, Minify, and Version my Javascript files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我肯定会投票将它们结合起来。如果您担心“未使用”Javascript 的解析或设置时间,那么我建议您使用闭包中的每个文件来构建 Javascript,然后在您需要的页面上运行您需要的闭包。例如:
然后,在您的页面中:
I would definitely vote to combine them. If you are concerned about parse or setup time for the "not used" Javascript, then I would recommend structuring your Javascript with each file in a closure, and then run the closures you need on the pages you need them. For example:
Then, in your page:
合并为 1 个文件。让它被缓存。它在任何页面上加载一次,并且对于任何后续页面,它可以使用缓存的副本。
combine into 1 file. let it get cached. it loads once on any page, and for any subsequent pages it can use the cached copy.
因为听起来不像有很多 javascript,所以将其合并到一个文件中会更好。仅当用户不访问页面时不需要加载大量 JavaScript 时,您才会考虑将文件分开。
Because it doesn't sound like there's a lot of javascript, combining it into one file would be better. Only if there's significant amounts of javascript that doesn't need to be loaded if a user doesn't visit a page then you would consider keeping the files separate.
它始终是平衡 HTTP 请求数量并限制尚未真正需要的传输字节的行为。
有三种可能性:
您只会知道什么通过进行一些 AB 负载测试最适合您的情况。
一切都取决于传输数据的大小、所需功能的重叠以及需要某些功能的概率。
It is always an act of balancing the number of HTTP requests and limiting the transferred bytes that are not really needed yet.
There are three possibilities:
You will only know what is best for your situation by doing some A-B load testing.
Everything depends on the size of the transferred data, the overlap of needed functionality and the probability that some functionality is needed.
如果合并后的文件小于 25kb,那么就继续使用吧。但如果不止于此,我会说,找出其中最大的一个,并将该 js 文件分开。结合其余部分。 25kb 限制也不是一个硬性规则,这取决于你。
如果您的单个文件的大小约为 30kb,我建议不要将它们组合起来,并让单个 js 文件缓存为单个 js 文件。
希望有帮助。
If the combined file is under say, 25kb minified, then go for it. But if it is more than that, I'd say, identify the one that is the biggest of them, and let that one js file be separate. Combine the rest. That 25kb limit thingy too is not a hard rule, it is up to you.
If your individual files are in the magnitude of say, 30kb, I'd recommend not combining them, and letting the individual js files be cached as individual js files.
Hope that helps.