Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

性能优化之懒执行 #7

Open
barretlee opened this issue Mar 2, 2017 · 2 comments
Open

性能优化之懒执行 #7

barretlee opened this issue Mar 2, 2017 · 2 comments

Comments

@barretlee
Copy link
Owner

barretlee commented Mar 2, 2017

认领人须知

  • 懒执行的场景距离
  • 懒执行的通用方案整理
@barretlee barretlee mentioned this issue Mar 2, 2017
17 tasks
@barretlee barretlee added this to the 性能相关基础知识点研究 milestone Mar 2, 2017
@veah
Copy link

veah commented Mar 6, 2017

认领 @barretlee

@veah
Copy link

veah commented Mar 15, 2017

懒执行(Lazy Execution)

场景描述

简单来说,懒执行(Lazy Execution)就是对特定功能逻辑的初始化进行全部或部分的延迟,直到满足某一触发条件,再进行剩余部分的初始化。

懒执行的主要作用在于性能优化,再确切一点应该是首屏优化。在首屏中存在某些模块或者功能逻辑,首屏加载过程中并不会被立即使用或者使用频率非常低。为了使首屏加载性能达到最优,此时对上述这种低需或者低频的功能逻辑可以延迟进行初始化,也就是所谓的懒执行。

场景距离的含义我不是很明白,还是得请小胡子哥补充

通用方案

某一业务功能如果被设置了懒执行,则需要满足特定条件后该功能才会被初始化(唤醒)。

因此,从剩余逻辑被初始化的触发(唤醒)方式上来看,懒执行的通用方案可以大致上分为两个思路:自动唤醒手动唤醒

  • 自动唤醒

    • 自动唤醒的核心思想是轮询,实现方法则是依靠 setInterval() 方法。
    • 首先需要定义判断条件 A,作为剩余逻辑的唤醒值。
    • 其次,声明一个监听函数 monitor() 作为 setInterval() 的回调函数,其基本思想是根据唤醒条件 A 的满足与否来决定是否执行唤醒动作:若是则唤醒,并 clearInterval 跳出轮询,否则继续轮询,直到条件满足。

    从上述分析来看,自动唤醒的方案需要轮询逻辑作为支撑,本身占用一定资源。因此自动唤醒应当被用于大型逻辑模块的懒执行方案中,否则有可能在性能上会得不偿失。

  • 手动唤醒

    手动唤醒主要应用于页面的交互场景下,也就是通过监听用户在页面内的 clickhoverscroll 等基本交互行为来触发唤醒动作。

    手动唤醒无需设置唤醒条件,也就不需要定义轮询函数来监听,完全由用户手动控制逻辑的触发,更加节省资源。因此中小型模块的懒执行方案可以选用手动唤醒方案。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants