performance-optimizer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Performance Optimizer

性能优化指南

Overview

概述

Provides a systematic approach to application performance optimization across the full stack. Use when diagnosing slow page loads, high API latency, database bottlenecks, or scaling issues. Not a substitute for application-specific profiling -- always measure before optimizing.
提供全栈应用性能优化的系统化方法。适用于诊断页面加载缓慢、API延迟过高、数据库瓶颈或扩展问题等场景。请注意,本指南不能替代针对特定应用的性能分析——优化前务必先进行测量。

Quick Reference

快速参考

Performance Budgets

性能预算

MetricTargetCategory
Largest Contentful Paint (LCP)< 2.5sCore Web Vital
Interaction to Next Paint (INP)< 200msCore Web Vital
Cumulative Layout Shift (CLS)< 0.1Core Web Vital
First Contentful Paint (FCP)< 1.8sFrontend
Time to Interactive (TTI)< 3.8sFrontend
Total Blocking Time (TBT)< 200msFrontend
API Response Time (P95)< 500msBackend
Database Query Time (P95)< 100msDatabase
Server Response Time (TTFB)< 600msBackend
指标目标类别
Largest Contentful Paint (LCP)< 2.5s核心Web指标
Interaction to Next Paint (INP)< 200ms核心Web指标
Cumulative Layout Shift (CLS)< 0.1核心Web指标
First Contentful Paint (FCP)< 1.8s前端
Time to Interactive (TTI)< 3.8s前端
Total Blocking Time (TBT)< 200ms前端
API Response Time (P95)< 500ms后端
Database Query Time (P95)< 100ms数据库
Server Response Time (TTFB)< 600ms后端

Optimization Phases

优化阶段

PhaseFocusKey Action
1. ProfilingIdentify real bottlenecksChrome DevTools, React Profiler, EXPLAIN ANALYZE
2. DatabaseEliminate slow queriesStrategic indexes, fix N+1, connection pooling
3. CachingReduce redundant workRedis, HTTP headers, CDN for static assets
4. FrontendReduce bundle and render timeBundle analysis, code splitting, resource hints, lazy loading
5. BackendSpeed up API responsesServerless optimization, streaming, conditional requests, queues
6. MonitoringSustain performanceAPM tools, alerting thresholds, dashboards
阶段重点方向关键行动
1. 性能分析识别真实瓶颈Chrome DevTools, React Profiler, EXPLAIN ANALYZE
2. 数据库调优消除慢查询策略性索引、修复N+1问题、连接池配置
3. 缓存优化减少重复工作Redis, HTTP headers, 静态资源CDN加速
4. 前端优化减少包体积与渲染时间包分析、代码分割、资源提示、懒加载
5. 后端优化提升API响应速度无服务器优化、流式传输、条件请求、队列处理
6. 性能监控维持性能水平APM工具、告警阈值、仪表盘配置

Caching Layers

缓存层级

LayerScopeDuration
Browser CacheHTTP headersStatic assets: 1 year (immutable); HTML: no-cache
CDNCloudflare, CloudFrontSame as browser, purge on deploy
ApplicationRedis, MemcachedVaries (e.g., 1 hour for user data)
DatabaseQuery cacheAutomatic
层级适用范围有效期
浏览器缓存HTTP headers静态资源:1年(不可变);HTML:不缓存
CDN缓存Cloudflare, CloudFront与浏览器缓存规则一致,部署时清除缓存
应用层缓存Redis, Memcached视情况而定(例如用户数据缓存1小时)
数据库缓存查询缓存自动生效

Common Mistakes

常见误区

MistakeCorrect Pattern
Optimizing before profilingMeasure first with Chrome DevTools, EXPLAIN ANALYZE, or APM tools to find real bottlenecks
Adding indexes on every columnUse strategic indexes on columns in WHERE, ORDER BY, and JOIN clauses; monitor with slow query log
SELECT * on large tablesSelect only needed columns to reduce I/O and memory
N+1 queries in loopsEager loading or DataLoader batching
Functions in WHERE clauseStore normalized values, use generated columns to preserve index usage
Caching without an invalidation strategyDefine TTL and invalidate-on-write policies; stale cache is worse than no cache
Loading entire libraries for a single utilityUse direct imports and tree-shaking
Running heavy computations synchronously in request handlersOffload to background job queues (BullMQ) and return immediately
误区正确做法
未进行性能分析就直接优化先使用Chrome DevTools、EXPLAIN ANALYZE或APM工具进行测量,找到真实瓶颈后再优化
为所有字段添加索引仅在WHERE、ORDER BY和JOIN子句涉及的字段上创建策略性索引;通过慢查询日志监控索引使用情况
在大表上使用SELECT *查询仅选择所需字段,以减少I/O和内存占用
循环中出现N+1查询问题使用预加载或DataLoader批量处理查询
在WHERE子句中使用函数存储标准化值,使用生成列以保留索引可用性
缓存未配置失效策略定义TTL(生存时间)和写时失效策略;过期缓存比无缓存的危害更大
为单个功能加载整个库使用直接导入和摇树优化(tree-shaking)
在请求处理程序中同步执行繁重计算将任务卸载到后台作业队列(如BullMQ)并立即返回响应

Delegation

任务分工

When working on performance optimization, delegate to:
  • frontend-builder
    -- React-specific performance patterns
  • application-security
    -- Rate limiting and DDoS protection
  • ci-cd-architecture
    -- Build pipeline optimization
进行性能优化工作时,可将以下任务分工给对应角色:
  • frontend-builder
    -- React专属性能优化方案
  • application-security
    -- 限流与DDoS防护
  • ci-cd-architecture
    -- 构建流水线优化

References

参考资料

  • Profiling and Measurement -- Chrome DevTools, React Profiler, Node.js/Python profiling, database EXPLAIN
  • Database Optimization -- Strategic indexes, N+1 fixes, query optimization, connection pooling
  • Caching Strategies -- Redis patterns, HTTP cache headers, CDN configuration
  • Frontend Performance -- Bundle analysis, code splitting, resource hints, third-party scripts, mobile performance, React patterns
  • Backend Performance -- Serverless optimization, streaming responses, conditional requests, background queues, rate limiting
  • Monitoring and Alerting -- APM tools, custom monitoring, dashboards, alert thresholds
  • 性能分析与测量 -- Chrome DevTools, React Profiler, Node.js/Python性能分析, 数据库EXPLAIN语句
  • 数据库优化 -- 策略性索引、N+1问题修复、查询优化、连接池配置
  • 缓存策略 -- Redis使用模式、HTTP缓存头配置、CDN设置
  • 前端性能优化 -- 包分析、代码分割、资源提示、第三方脚本优化、移动端性能、React优化方案
  • 后端性能优化 -- 无服务器优化、流式响应、条件请求、后台队列、限流
  • 监控与告警 -- APM工具、自定义监控、仪表盘、告警阈值