简体   繁体   English

异步组件会加快加载时间吗?

[英]Do async components speed up load time?

I am using Vue and Webpack in a large project and I am looking to optimize the loading of some components.我在一个大型项目中使用 Vue 和 Webpack,我希望优化一些组件的加载。 I am already lazy loading my pages but I wonder if I can take a step further and dynamically load subcomponents inside those pages.我已经懒得加载我的页面了,但我想知道我是否可以更进一步,在这些页面中动态加载子组件。

I want to load a subcomponent ("my-modal" in the exemple below) after the main component (the page) is rendered (to speed up the load time of the page).我想在渲染主组件(页面)之后加载一个子组件(以下示例中的“my-modal”)(以加快页面的加载时间)。 This sub-component is relatively heavy and means to be frequently open, but is hidden by default (its a modal).这个子组件比较重,意味着经常打开,但默认是隐藏的(它是一个模态的)。 So my strategy is to load it right after the page is loaded but not with the page, so the page can load faster.所以我的策略是在页面加载立即加载,而不是页面一起加载,这样页面加载速度会更快。

I came across vue's Async Components but I am not sure if it can help, does-it ?我遇到了 vue 的异步组件,但我不确定它是否有帮助,是吗?

Here is an exemple of what I was doing with an old school "static" import :这是我使用老式“静态”导入所做的示例:

// page.vue

<script>
import myModal from "@/components/my-modal";

export default {
  components: {
    myModal,
  },
}
</script>

Here is an exemple of how I am dynamically importing my sub-component (aka "async components") :这是我如何动态导入子组件(又名“异步组件”)的示例:

// page.vue

<script>
export default {
  components: {
    myModal: () => import("@/components/my-modal"),
  },
}
</script>

I am registering my component locally ( local registration ), but I am not sure if its the best way, maybe its another question...我正在本地注册我的组件( 本地注册),但我不确定它是否是最好的方法,也许是另一个问题......

Importing dynamically will generate a new file, and this new file will only be downloaded when the user goes to that specific page, not when the user goes to / .动态导入会生成一个新文件,并且这个新文件只会在用户进入该特定页面时下载,而不是在用户进入/时下载。

That increases load speed and decreases the bytes that the user has to download at the moment one access your site.这提高了加载速度并减少了用户在访问您的站点时必须下载的字节数。

For all intents and purposes...probably not.出于所有意图和目的……可能不是。

With today's modern devices, browsers, and Internet access, that component would have to be pretty significant in order to provide a speedup overall to page load.对于当今的现代设备、浏览器和 Internet 访问,该组件必须非常重要才能提供整体页面加载速度。 If it's just markup and code, I'd have a hard time believing that one component would even come close to approaching say, 700Kb in size (especially if minified and compressed).如果只是标记和代码,我很难相信一个组件的大小甚至会接近 700Kb(尤其是在缩小和压缩的情况下)。 In all but the worst Internet connectivity, the browser is going to handle this with no problem at all, especially if the component is include in the main bundle for the site.除了最差的 Internet 连接外,浏览器都可以毫无问题地处理这个问题,特别是如果该组件包含在站点的主包中。

However, if your component does some heavy computational work upon arrival, then maybe there would be a speedup to loading it async (as the browser can "get on with" loading the page while waiting for the modal to arrive).但是,如果您的组件在到达时执行了一些繁重的计算工作,那么异步加载它可能会加快速度(因为浏览器可以在等待模式到达时“继续”加载页面)。

However however , most browsers only allow a number of XHR requests running to a single domain at a time (I think Chrome is 4 by default).然而,大多数浏览器一次只允许多个 XHR 请求运行到单个域(我认为 Chrome 默认为 4 个)。 Lazy loading a file would mean the requests made by the page itself take priority, but having more files to load over multiple requests rather than lumped into one may be overall slower.延迟加载文件意味着页面本身发出的请求具有优先权,但是在多个请求上加载更多文件而不是集中到一个请求中可能总体上会更慢。

However however however , browsers are excellent at caching these requests, so subsequent visits to your site probably wouldn't have this problem to consider.然而浏览器在缓存这些请求方面非常出色,因此后续访问您的站点可能不会考虑这个问题。

Lazy loading modules really provides a speedup when you are able to lazy load entire sections of your site only when the user needs to access them, like on routes.延迟加载模块确实提供了加速,当您能够延迟加载站点的整个部分时,只有当用户需要访问它们时,例如在路线上。

The final however , though, is that it's good practice to not burden browsers and mobile devices with needless bloat, and while lazy loading that module may not have a noticeable speed difference, it may overall be a decent idea depending on your needs and when you load it.然而,最后一点是,最好不要给浏览器和移动设备带来不必要的臃肿负担,虽然延迟加载该模块可能没有明显的速度差异,但根据您的需要和何时加载它。 If a visitor never needs to view the modal, and you load the modal when it's needed...there's a lot to consider, as that may cause a delayed UI but be better for an overall greater percent of your users.如果访问者从不需要查看模态,并且您在需要时加载模态...有很多需要考虑的因素,因为这可能会导致 UI 延迟,但对整体更大比例的用户来说会更好。

If you're trying to make your site more accessible to those with poor Internet, and you know the modal will be used frequently, it'd be probably better to load it with the page because it's part of page functionality.如果你想让你的网站更容易被那些互联网很差的人访问,并且你知道模式会被频繁使用,那么最好将它与页面一起加载,因为它是页面功能的一部分。

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

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