• 前端性能优化:如何根据chrome的timing优化


    性能优化API

    • Performanceperformance.now()new Date()区别,它是高精度的,且是相对时间,相对于页面加载的那一刻。但是不一定适合单页面场景
    • window.addEventListener("load", ""); window.addEventListener("domContentLoaded", "");
    • Imgonload事件,监听首屏内的图片是否加载完成,判断首屏事件
    • RequestFrameAnmationRequestIdleCallback
    • IntersectionObserverMutationObserverPostMessage
    • Web Worker,耗时任务放在里面执行

    检测工具

    • Chrome Dev Tools
    • Page Speed
    • Jspref

    前端指标

    在这里插入图片描述

    window.onload = function(){
        setTimeout(function(){
            let t = performance.timing
            console.log('DNS查询耗时 :' + (t.domainLookupEnd - t.domainLookupStart).toFixed(0))
            console.log('TCP链接耗时 :' + (t.connectEnd - t.connectStart).toFixed(0))
            console.log('request请求耗时 :' + (t.responseEnd - t.responseStart).toFixed(0))
            console.log('解析dom树耗时 :' + (t.domComplete - t.domInteractive).toFixed(0))
            console.log('白屏时间 :' + (t.responseStart - t.navigationStart).toFixed(0))
            console.log('domready时间 :' + (t.domContentLoadedEventEnd - t.navigationStart).toFixed(0))
            console.log('onload时间 :' + (t.loadEventEnd - t.navigationStart).toFixed(0))
    
            if(t = performance.memory){
                console.log('js内存使用占比 :' + (t.usedJSHeapSize / t.totalJSHeapSize * 100).toFixed(2) + '%')
            }
        })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    DNS预解析优化

    dns解析是很耗时的,因此如果解析域名过多,会让首屏加载变得过慢,可以考虑dns-prefetch优化

    DNS Prefetch 应该尽量的放在网页的前面,推荐放在 后面。具体使用方法如下:

    <meta http-equiv="x-dns-prefetch-control" content="on">
    <link rel="dns-prefetch" href="//www.zhix.net">
    <link rel="dns-prefetch" href="//api.share.zhix.net">
    <link rel="dns-prefetch" href="//bdimg.share.zhix.net">
    
    • 1
    • 2
    • 3
    • 4

    request请求耗时

    • 不请求,用cache(最好的方式就是尽量引用公共资源,同时设置缓存,不去重新请求资源,也可以运用PWA的离线缓存技术,可以帮助wep实现离线使用)
    • 前端打包时压缩
    • 服务器上的zip压缩
    • 图片压缩(比如tiny),使用webp等高压缩比格式
    • 把过大的包,拆分成多个较少的包,防止单个资源耗时过大
    • 同一时间针对同一域名下的请求有一定数量限制,超过限制数目的请求会被阻塞。如果资源来自于多个域下,可以增大并行请求和下载速度
    • 延迟、异步、预加载、懒加载
    • 对于非首屏的资源,可以使用 defer 或 async 的方式引入
    • 也可以按需加载,在逻辑中,只有执行到时才做请求
    • 对于多屏页面,滚动时才动态载入图片
  • 相关阅读:
    计算机毕业设计Python+Django的学生考勤管理系统(源码+系统+mysql数据库+Lw文档)
    关于linux openssl的自签证书认证与nginx配置
    tensorRT简明使用
    Spring Boot 工程开发常见问题解决方案,日常开发全覆盖
    【网络安全】Docker部署DVWA靶机环境
    015 Linux 标准输入输出、重定向、管道和后台启动进程命令
    Q41F-40C手动球阀型号解析
    C++STL详解(二)vector的使用及其模拟实现
    NoSQL数据库:定义、特性、分类与应用场景的探索
    CEC2015:动态多目标测试函数之FDA4、FDA5、FDA5_iso、FDA5_dec
  • 原文地址:https://blog.csdn.net/php_martin/article/details/125873828