前端性能优化有哪些常见手段?

加载优化

1. 减少请求

2. 压缩

3. 缓存

4. 按需加载

// 路由懒加载
const Home = lazy(() => import('./Home'))

// 图片懒加载
<img loading="lazy" src="image.jpg" />

// 动态导入
const module = await import('./heavy-module')

5. 预加载

<!-- 预加载关键资源 -->
<link rel="preload" href="/critical.js" as="script">
<link rel="preload" href="/font.woff2" as="font" crossorigin>

<!-- 预连接 -->
<link rel="preconnect" href="https://cdn.example.com">
<link rel="dns-prefetch" href="https://cdn.example.com">

<!-- 预获取(空闲时) -->
<link rel="prefetch" href="/next-page.js">

渲染优化

1. CSS 优化

2. JS 优化

3. 减少重排重绘

4. 虚拟列表

// react-window
import { FixedSizeList } from 'react-window'
<FixedSizeList height={600} itemCount={10000} itemSize={35}>
  {Row}
</FixedSizeList>

图片优化

<!-- 响应式图片 -->
<img src="small.jpg" 
     srcset="medium.jpg 768w, large.jpg 1200w"
     sizes="(max-width: 768px) 100vw, 50vw">

<!-- 现代格式 -->
<picture>
  <source srcset="image.avif" type="image/avif">
  <source srcset="image.webp" type="image/webp">
  <img src="image.jpg">
</picture>

构建优化

// Vite 构建
import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['react', 'react-dom'],
          utils: ['lodash-es', 'dayjs']
        }
      }
    },
    minify: 'terser',
    cssCodeSplit: true
  }
})

性能指标

指标 含义 目标
FCP 首次内容绘制 < 1.8s
LCP 最大内容绘制 < 2.5s
FID 首次输入延迟 < 100ms
CLS 累积布局偏移 < 0.1
TTI 可交互时间 < 3.8s

检测工具

同分类其他题目