关于前端优化

前端性能优化指标 RAIL

RAIL 测量模型

RAIL 评估标准

性能测量工具

性能测试API

Performance API

Performance - Web API 接口参考 | MDN - Mozilla Developer

// 计算一些关键的性能指标
window.addEventListener('load', (event) => {
    // Time to Interactive
    let timing = performance.getEntriesByType('navigation')[0];
    console.log(timing.domInteractive);
    console.log(timing.fetchStart);
    let diff = timing.domInteractive - timing.fetchStart;
    console.log("TTI: " + diff);
})
// 观察长任务
const observer = new PerformanceObserver((list) => {
    for (const entry of list.getEntries()) {
        console.log(entry)
    }
})

observer.observe({entryTypes: ['longtask']})
// 见面可见性的状态监听
let vEvent = 'visibilitychange';
if (document.webkitHidden != undefined) {
    // webkit prefix detected
    vEvent = 'webkitvisibilitychange';
}

function visibilityChanged() {
    if (document.hidden || document.webkitHidden) {
        console.log("Web page is hidden.")
    } else {
        console.log("Web page is visible.")
    }
}

document.addEventListener(vEvent, visibilityChanged, false);
// 网络状态
var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
var type = connection.effectiveType;

function updateConnectionStatus() {
  console.log("Connection type changed from " + type + " to " + connection.effectiveType);
  type = connection.effectiveType;
}

connection.addEventListener('change', updateConnectionStatus);

为什么会出现白屏?

白屏时间指的是浏览器开始显示内容的时间,因此,我们通常认为浏览器开始渲染 <body> 标签或者解析完 <head> 标签的时刻就是页面白屏结束的时间点。

通常白屏是以下原因导致的:

检测页面白屏时长

如何优化 canvas 性能

developer.mozilla.org-canvas 的优化

CSS 3D 加速

React 优化

JIT 优化

A crash course in just-in-time (JIT) compilers

为什么 ESBuild 会那么快

Esbuild 为什么那么快 - 知乎