外部 JavaScript 文件不会导致更多的客户端处理吗?
我正在考虑外部 HTML 文件,我突然想到,如果我将多个 HTML 页面的函数分组,在一个 JavaScript 中,这将导致额外的客户端处理。
基本上,我想知道这是否正确。
这是我的想法。假设我有一个包含 5 页的 JavaScript 文件。如果用户访问每个页面,则对于每个页面,他不仅必须加载该页面的 JavaScript,还必须加载其他四个页面的 JavaScript。最终总和是用户浏览器加载的 JavaScript 数量大约是正常情况下的 5 倍。
我认为大多数人都按照常见功能对 JavaScript 进行分组。因此,您可以拥有多个页面的 JavaScript 文件,但您可能不会在每个页面上使用所有 JavaScript。因此,您不在每个页面上使用的所有 JavaScript 都无需运行/加载。
我有一个子问题。我知道您不必为每个页面重新加载 JavaScript 文件。 JavaScript 文件是否每次都运行? JavaScript 是否重新加载?通过重新加载,我的意思是每次浏览器必须从缓存中获取文件时会产生什么样的开销?
谢谢, 格雷
I was thinking about external HTML files, and it occured to me, that if I group the functions from several HTML pages, in one JavaScript this will lead to extra clientside processing.
Basically, I would like some idea of whether this is correct.
Here is my thinking. Suppose I have one JavaScript file for five pages. If the user goes to each page, for each page he has to load not only the JavaScript for that page, but the JavaScript for the other four pages. The final sum is the user's browser loaded about 5 times as much JavaScript as he would have normally.
I think most people group there JavaScript by common functionality. So you can have the JavaScript file with several pages, however you may not use all the JavaScript on every page. So all the JavaScript you don't use on every page is run/loaded without need.
I have a sub-question. I know you don't have to redoanload the JavaScript file for each page. Is the JavaScript file run each time? Is the JavaScript reloaded? By reloaded, I mean what kind of over head is there for each time the browse has to get a file out of the cache?
Thanks,
Grae
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我有一个 200 行的文件,并将其分成 5 个文件,每个文件 40 行,则总行数仍为 200 BUT。请记住,如果我在上一页上拉取文件 1-4,那么我现在只需要拉取文件 5,因为 1-4 在我的缓存中。此外,大多数现代浏览器都会对这些请求进行线程处理,因此我不会对单个文件进行单个大文件下载,而是对较小文件进行 5 个线程下载。
浏览器的开销将取决于浏览器如何处理它,并且在具体实现方面超出了我的想象。
If I have a file of 200 lines, and seperate it out to 5 files of 40 lines each, the total number of lines remains at 200 BUT. remember that, if I pulled files 1-4 on the previous page, I only now need to pull file 5 since 1-4 are in my cache. additionally, most modern browsers are goint to thread those requests so instead of a single large file download for a single file, I get 5 threaded downloads of smaller files.
the overhead for the browsers would be pretty browser specific in how they handle it and above my head on exact implementation.
如果缓存设置正确,则相反:文件将仅加载一次,在用户访问网站之初。在大多数情况下,要加载的数据总量将会减少。
所有四个页面的 JavaScript 代码将以某种方式加载到浏览器的内存中,甚至可能预先解析(我不知道具体细节),但这部分脚本处理完全可以忽略不计。
将 JS 库分成多个块可能仍然是明智的,如果它们在每四个页面上完全独立并且非常巨大 - 这将取决于您的脚本的结构。但大多数情况下,拥有一个外部文件,因此第一次请求稍大一些,但之后就不再请求,是更好的方法。
对于您的子问题,请查看 Firebug 的“网络”选项卡。它将向您显示它加载的资源、从何处加载以及处理它们需要多长时间。
If caching is set up correctly, the contrary will be true: The file will be loaded only once, at the beginning of the user's visiting the site. The overall amount of data to load will be reduced in most cases.
The JavaScript code for all four pages will be loaded in the browser's memory somehow, maybe even pre-parsed (I don't know the exact specifics of this), but that part of script processing is totally negligible.
It could still be wise to split your JS library into chunks, if they are totally separate on every four pages and really huge - it will depend on your script's structure. But mostly, having one external file, and therefore one slightly bigger request the first time but none afterwards, is the preferable way.
For your sub-question, take a look at Firebug's "Net" tab. It will show you which resources it loads and from where, and how long it takes to process them.
最好将所有页面的 javascript 打包到一个文件中。该文件将被缓存,浏览器不会在连续请求时再次下载。原因是,对于服务器和客户端来说,发出 Web 请求的成本远高于客户端解析 javascript 文件的成本。
如今的浏览器速度如此之快,您不必担心客户端必须加载一些可能不会用于该特定页面的额外 JavaScript。
为了使您的网站速度更快,您应该集中精力将请求量保持在绝对最低限度。
It's better to pack the javascript for all pages into one file. The file will be cached and not downloaded again by the browser for consecutive requests. The reason is that making a web request is far more expensive for your server and the client than for the client to parse the javascript-file.
Browsers are so fast these days that you don't have to worry about the client having to load some extra javascript that might not be used for that specific page.
To make your site fast, you should focus on keeping the amount of requests to an absolute minimum.