Git Product home page Git Product logo

Comments (2)

veah avatar veah commented on June 18, 2024

认领 @barretlee

from performance-column.

veah avatar veah commented on June 18, 2024

懒执行(Lazy Execution)

场景描述

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

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

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

通用方案

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

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

  • 自动唤醒

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

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

  • 手动唤醒

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

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

from performance-column.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.