首页 > 编程语言 >[Javascript Vue] Improve heavy component loading performance

[Javascript Vue] Improve heavy component loading performance

时间:2024-09-15 20:46:25浏览次数:1  
标签:heavy function loading return Javascript performance frameCount

Let's say we have a Vue application that renders many heavy components on the first load. The problem we're facing is a long white screen period while JavaScript is loading and the browser is painting.

How can we optimize performance?

Since the page size is limited, we should only render what's visible to the user during the first load and defer the rendering of other components. This way, the user perceives the application as loading faster.

import {onUnmounted, ref} from 'vue'

export function useDefer(maxCount = 100) {
    const frameCount = ref(0)
    let refId;
    function updateFrameCount() {
        refId = requestAnimationFrame(() => {
            frameCount.value++;
            if (frameCount.value >= maxCount) {
                return;
            }
            updateFrameCount();
        });
    }
    updateFrameCount();
    onUnmounted(() => {
        cancelAnimationFrame(refId)
    })
    return function defer(n) {
        // return true then component should be renderered.
        // return false then not.
        return frameCount.value >= n;
    }
}
<template>
    <div class="container">
        <div v-for="n in 100">
            <heavy-comp v-if="defer(n)"></heavy-comp>
        </div>
    </div>
</template>

 

标签:heavy,function,loading,return,Javascript,performance,frameCount
From: https://www.cnblogs.com/Answer1215/p/18415611

相关文章

  • JavaScript DOM
    一、DOM简介1、简介​DocumentObjectModel文档对象模型​浏览器加载HTML文档时,会将HTML文档解析为一个树形结构,称为DOM树HTML文档和DOM树是一一对应的关系当DOM树被改变时,与之对应的HTML文档也会随之改变当需要对HTML中的内容进行动态改变时,可以使用DOM来进行操作DOM......
  • JavaScript中if嵌套 assert
    摘要: 本文主要探讨在JavaScript中if嵌套的使用场景以及assert语句在代码调试与逻辑验证方面的作用。通过分析if嵌套的结构与常见用法,结合assert语句在确保程序正确性上的优势,阐述它们在JavaScript编程中的重要性与高效运用方式。一、引言在JavaScript开发中,控制结构......
  • 蓝易云服务器 - ubuntu安装开发javascript ubuntu script教程
    在Ubuntu上安装开发JavaScript的教程如下:打开终端。安装Node.js:运行以下命令安装Node.js。sudoaptupdatesudoaptinstallnodejs安装npm:npm是Node.js的包管理器,运行以下命令安装npm。sudoaptinstallnpm验证安装:通过运行以下命令验证Node.js和npm是否安装成功。node-vnpm-......
  • JavaScript 中的异步任务、同步任务、宏任务与微任务
    JavaScript中的异步任务、同步任务、宏任务与微任务在JavaScript的世界里,理解异步任务、同步任务、宏任务和微任务是非常重要的,它们共同构成了JavaScript独特的执行机制。一、同步任务与异步任务1.同步任务定义:同步任务是在代码执行过程中,按照顺序依次执行的任务......
  • 【JavaScript】LeetCode:707设计链表
    文章目录题目内容题目分析(1)获取第n个节点的值(2)头部插入节点(3)尾部插入节点(4)第n个节点前插入节点(5)删除第n个节点完整代码题目内容题目分析添加哨兵节点dummy。在第n个节点前插入节点时,应该找到第n-1个节点(即前一个节点),才能完成插入操作。在删除第n......
  • 【工具】前端JavaScript代码在线执行器 方便通过网页 手机测试js代码
    【工具】前端JavaScript代码在线执行器方便通过网页手机测试js代码自动补全js代码格式化代码色彩打印日志清空日志待补充<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,ini......
  • Error while loading conda entry point: anaconda-cloud-auth (cannot import name
    这个错误是由于conda环境中的某些插件或依赖损坏,特别是在conda.plugins.types模块中无法找到ChannelAuthBase。这通常发生在conda安装不完整、升级失败或插件包损坏的情况下。可能的解决方案:1.更新conda首先尝试更新conda,这可以修复一些与依赖相关的问题:condaupdatecon......
  • JavaScript语法入门六 数据类型
    数据类型JavaScript数据类型有8种,分别是number、bigint、string、boolean、null、undefined、symbol、object。JavaScript是一种弱类型语言,或者说动态类型语言。即每一个变量的类型在定义之后可变化的,JavaScript根据使用情况自动识别。number类型整数、浮点数。范围:常规的数字、Inf......
  • JavaScript空值判断
    JavaScript本身没有判断一个变量是不是空值的函数,因为变量有可能是string,object,number,boolean等类型,类型不同,判断方法也不同。所以在文章中写了一个函数,用以判断JS变量是否空值,如果是undefined,null,'',NaN,false,0,[],{},空白字符串,都返回true,否则返回false。functionisEmpty(v){sw......
  • WPF 实现一个吃豆豆的Loading加载动画
    运行的效果如下 先引入一下我们需要的库在nuget上面搜一下"expression.Drawing",安装一下这个包 我们再创建一个Window,引入一下这个包的命名空间我们设置一下这个加载动画呈现的窗体的样式xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"......