• 前端性能优化:如何根据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 的方式引入
    • 也可以按需加载,在逻辑中,只有执行到时才做请求
    • 对于多屏页面,滚动时才动态载入图片
  • 相关阅读:
    brew 常用命令
    概论(二)随机变量
    9.4黄金行情是否反转?今日多空如何布局?
    MySQL运维实战之备份和恢复(8.7)将数据库恢复到指定时间点的另外一种方法
    《Java并发编程的艺术》读书笔记 - 第三章 - Java内存模型
    Day 62 单向循环链表 双链表
    vue自动触发点击
    【C语言】typedef是什么东东?指针详情回忆不起来啦(每日小细节005)
    文件备份、数据库备份
    很详细的Django开发入门详解(图文并茂)
  • 原文地址:https://blog.csdn.net/php_martin/article/details/125873828