简体   繁体   English

单个.js文件与延迟加载

[英]Single .js File vs. Lazy Loading

Scenario: You are building a large javascript-driven web application where you want as few page refreshes as possible. 场景:您正在构建一个大型的javascript驱动的Web应用程序,您希望尽可能少的页面刷新。 Imagine 80-100MB of unminified javascript, just for the sake of having a number to define "large". 想象一下80-100MB的无限制javascript,只是为了有一个数字来定义“大”。

My assumption is that if you lazy-load your javascript files you can get a better balance on your load times (meaning, you don't have to wait a few seconds each time the page refreshes), hopefully resulting in the user not really noticing a lag during loading. 我的假设是,如果你懒加载你的javascript文件,你可以更好地平衡你的加载时间(意思是,你不必每次页面刷新时等待几秒钟),希望导致用户没有真正注意到装载时滞后。 I'm guessing that in a scenario like this, lazy-loading would be more desireable than your typical single minified .js file. 我猜测在这样的场景中,延迟加载比典型的单个缩小的.js文件更令人满意。

Now, theoretically, there is a fixed cost for a request to any file on a given server, regardless of the file's size. 现在,理论上,无论文件的大小如何,对给定服务器上的任何文件的请求都有固定的成本。 So, too many requests would not be desirable. 因此,太多的请求是不可取的。 For example, if a small javascript file loads at the same time as 10 other small- or medium-sized files, it might be better to group them together to save on the cost of multiple requests. 例如,如果一个小的javascript文件与其他10个小型或中型文件同时加载,最好将它们组合在一起以节省多个请求的成本。

My question is, assuming reasonable defaults (say the client has a 3-5Mbps connection and a decent piece of hardware), what is an ideal size of a file to request? 我的问题是,假设合理的默认值(比如客户端有3-5Mbps的连接和一块体面的硬件), 请求的文件的理想大小是多少? Too large, and you are back to loading too much at one time; 太大了,你一次又回来装载太多了; too small, and the cost of the request becomes more expensive than the amount of data you are getting back, reducing your data-per-second economy. 太小了,请求的成本变得比您回来的数据量更高,从而降低了每秒数据的经济性。

Edit : All the answers were fantastic. 编辑 :所有答案都很棒。 I only chose Ben's because he gave a specific number. 我只选择了Ben,因为他给出了一个特定的号码。

I would try to keep the amount that needs to be loaded to show the page (even if just the loading indicator) under 300K. 我会尝试保持需要加载的金额以显示300K以下的页面(即使只是加载指示器)。 After that, I would pull down additional data in chunks of up to 5MB at a time, with a loading indicator (maybe a progress bar) shown. 之后,我会一次性下载最多5MB的数据块,并显示加载指示符(可能是进度条)。 I've had 15MB downloads fail on coffee shop broadband wifi that otherwise seemed OK. 我已经有15MB的下载失败在咖啡店宽带wifi,否则似乎没问题。 If it was bad enough that <5MB downloads failed I probably wouldn't blame a website for not working. 如果足够糟糕,<5MB下载失败,我可能不会责怪网站不工作。

I also would consider downloading two files at a time, beyond the initial <300K file, using a loader like LabJS or HeadJS to programmatically add script tags. 我还会考虑一次下载两个文件,超出最初的<300K文件,使用像LabJSHeadJS这样的加载器以编程方式添加脚本标记。

I think it's clear that making the client download more than a MB of js before they can do anything is bad. 我认为很明显,让客户端下载超过MB的js然后才能做任何事情都是不好的。 And also making the client download more of anything than is necessary is also bad. 并且让客户端下载更多不必要的东西也是不好的。 But there's a clear benefit to having it all cached. 但是将所有缓存都清理好了。

Factors that will influence the numbers: 影响数字的因素:

  1. Round-trip time 往返时间
  2. Server response time 服务器响应时间
  3. Header Size (including cookies) 标题大小(包括cookie)
  4. Caching Technique 缓存技术
  5. Browser (see http://browserscope.com ) 浏览器(参见http://browserscope.com

Balancing parallel downloading and different cache requirements are also factors to worry about. 平衡并行下载和不同的缓存要求也是需要担心的因素。 This was partially covered recently by Kyle Simpson here: http://www.reddit.com/r/javascript/comments/j7ng4/do_you_use_script_loaders/c29wza8 Kyle Simpson最近在此部分介绍了这一点: http//www.reddit.com/r/javascript/comments/j7ng4/do_you_use_script_loaders/c29wza8

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

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