Javascript - 结合还是不结合,这就是问题

发布于 2024-09-18 04:17:53 字数 1002 浏览 5 评论 0原文

好的,所以我知道为了提高效率,将所有页面 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.js

Contact.htm
jquery.js
contact.js

Faq.htm
jquery.js
faq.js

Option: 2

Default.htm
jquery-default-contact-faq-min.js

Contact.htm
jquery-default-contact-faq-min.js

Faq.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

°如果伤别离去 2024-09-25 04:17:53

我肯定会投票将它们结合起来。如果您担心“未使用”Javascript 的解析或设置时间,那么我建议您使用闭包中的每个文件来构建 Javascript,然后在您需要的页面上运行您需要的闭包。例如:

// File 1
window.closures = window.closures || {}
window.closures["page1"] = (function() {
  // Javascript for Page 1
});

// File 2
window.closures = window.closures || {}
window.closures["page2"] = (function() {
  // Javascript for Page 2
});    

// File 3
window.closures = window.closures || {}
window.closures["page2"] = (function() {
  // Javascript for Page 2
});

然后,在您的页面中:

<!-- This one combined.js file will be downloaded once and cached //-->
<script type="text/javascript" src="combined.js"></script>
<script>
  // Run the Javascript in your combined.js intended for page2
  window.closures["page2"]()
</script>

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:

// File 1
window.closures = window.closures || {}
window.closures["page1"] = (function() {
  // Javascript for Page 1
});

// File 2
window.closures = window.closures || {}
window.closures["page2"] = (function() {
  // Javascript for Page 2
});    

// File 3
window.closures = window.closures || {}
window.closures["page2"] = (function() {
  // Javascript for Page 2
});

Then, in your page:

<!-- This one combined.js file will be downloaded once and cached //-->
<script type="text/javascript" src="combined.js"></script>
<script>
  // Run the Javascript in your combined.js intended for page2
  window.closures["page2"]()
</script>
忘你却要生生世世 2024-09-25 04:17:53

合并为 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.

剩余の解释 2024-09-25 04:17:53

因为听起来不像有很多 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.

拥抱没勇气 2024-09-25 04:17:53

它始终是平衡 HTTP 请求数量并限制尚未真正需要的传输字节的行为。

有三种可能性:

  1. 将所有内容合并在一个文件中
  2. 具有三个单独的文件,并根据需要加载它们
  3. 具有三个单独的文件,立即加载该页面所需的文件并预加载其他文件(当时间合适时)

您只会知道什么通过进行一些 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:

  1. combine everything in 1 file
  2. have three separate files, and load them as needed
  3. have three separate files, load the one needed for that page right away and preload the others (when the time is right)

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.

乖乖公主 2024-09-25 04:17:53

如果合并后的文件小于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文