performance-audit
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseResources
参考资源
scripts/
validate-performance-audit.sh
references/
performance-patterns.mdscripts/
validate-performance-audit.sh
references/
performance-patterns.mdPerformance Audit
性能审计
This skill guides you through performing comprehensive performance audits on web applications to identify bottlenecks, optimization opportunities, and performance regressions. Use this when preparing for production deployments, investigating performance issues, or conducting periodic performance reviews.
本技能将引导你对Web应用进行全面的性能审计,以识别性能瓶颈、优化机会和性能退化问题。适用于生产部署前准备、性能问题排查或定期性能评审场景。
When to Use This Skill
何时使用此技能
- Conducting pre-deployment performance reviews
- Investigating slow page loads or poor user experience
- Performing periodic performance audits on existing applications
- Validating performance after major feature additions
- Preparing for traffic scaling or load testing
- Optimizing Core Web Vitals for SEO and user experience
- 开展部署前的性能评审
- 排查页面加载缓慢或用户体验不佳的问题
- 对现有应用进行定期性能审计
- 新增重大功能后验证性能表现
- 为流量扩容或负载测试做准备
- 优化Core Web Vitals以提升SEO和用户体验
Audit Methodology
审计方法论
A systematic performance audit follows these phases:
系统的性能审计遵循以下阶段:
Phase 1: Reconnaissance
阶段1:侦察分析
Objective: Understand the application architecture, tech stack, and performance surface area.
Use discover to map performance-critical code:
yaml
discover:
queries:
- id: bundle_imports
type: grep
pattern: "^import.*from ['\"].*['\"]$"
glob: "src/**/*.{ts,tsx,js,jsx}"
- id: database_queries
type: grep
pattern: "(prisma\\.|db\\.|query\\(|findMany|findUnique|create\\(|update\\()"
glob: "**/*.{ts,tsx,js,jsx}"
- id: component_files
type: glob
patterns: ["src/components/**/*.{tsx,jsx}", "src/app/**/*.{tsx,jsx}"]
- id: api_routes
type: glob
patterns: ["src/app/api/**/*.{ts,tsx}", "pages/api/**/*.{ts,tsx}"]
verbosity: files_onlyIdentify critical components:
- Page entry points and route handlers
- Heavy npm dependencies (moment.js, lodash, etc.)
- Database access patterns and ORM usage
- Image and media assets
- API endpoints and data fetching logic
- Client-side rendering vs server components
目标: 了解应用架构、技术栈以及性能影响面。
使用discover工具映射性能关键代码:
yaml
discover:
queries:
- id: bundle_imports
type: grep
pattern: "^import.*from ['\"].*['\"]$"
glob: "src/**/*.{ts,tsx,js,jsx}"
- id: database_queries
type: grep
pattern: "(prisma\\.|db\\.|query\\(|findMany|findUnique|create\\(|update\\()"
glob: "**/*.{ts,tsx,js,jsx}"
- id: component_files
type: glob
patterns: ["src/components/**/*.{tsx,jsx}", "src/app/**/*.{tsx,jsx}"]
- id: api_routes
type: glob
patterns: ["src/app/api/**/*.{ts,tsx}", "pages/api/**/*.{ts,tsx}"]
verbosity: files_only识别关键组件:
- 页面入口点和路由处理器
- 体积较大的npm依赖(moment.js、lodash等)
- 数据库访问模式与ORM使用情况
- 图片与媒体资源
- API端点与数据获取逻辑
- 客户端渲染 vs 服务器组件
Phase 2: Bundle Analysis
阶段2:打包分析
Objective: Identify large dependencies, code splitting opportunities, and dead code.
目标: 识别大型依赖、代码拆分机会和死代码。
Check Bundle Size
检查打包体积
Run bundle analyzer:
yaml
precision_exec:
commands:
- cmd: "npm run build"
timeout_ms: 120000
- cmd: "npx @next/bundle-analyzer"
timeout_ms: 60000
verbosity: standardCommon issues:
- Large dependencies imported on every page (moment.js, lodash, chart libraries)
- Multiple versions of the same package (check with )
npm ls <package> - Unused dependencies still bundled (tree-shaking failures)
- SVG icons imported as components instead of sprite sheets
- Entire UI libraries imported instead of individual components
运行打包分析工具:
yaml
precision_exec:
commands:
- cmd: "npm run build"
timeout_ms: 120000
- cmd: "npx @next/bundle-analyzer"
timeout_ms: 60000
verbosity: standard常见问题:
- 每个页面都导入大型依赖(moment.js、lodash、图表库)
- 同一包存在多个版本(使用检查)
npm ls <package> - 未使用的依赖仍被打包(tree-shaking失败)
- SVG图标作为组件导入而非使用精灵图
- 导入整个UI库而非单个组件
Find Heavy Imports
查找重量级导入
Search for common performance-heavy packages:
yaml
precision_grep:
queries:
- id: heavy_deps
pattern: "import.*from ['\"](moment|lodash|date-fns|rxjs|@material-ui|antd|chart\\.js)['\"]"
glob: "**/*.{ts,tsx,js,jsx}"
- id: full_library_imports
pattern: "import .* from ['\"](lodash|@mui/material|react-icons)['\"]$"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: locationsOptimization strategies:
Bad - importing entire library:
typescript
import _ from 'lodash'; // 71KB gzipped
import * as Icons from 'react-icons/fa'; // 500+ iconsGood - importing specific modules:
typescript
import debounce from 'lodash/debounce'; // 2KB gzipped
import { FaUser, FaCog } from 'react-icons/fa'; // Only what you needBetter - using modern alternatives:
typescript
// Instead of moment.js (72KB), use date-fns (13KB) or native Intl
import { format } from 'date-fns';
// Or native APIs
const formatted = new Intl.DateTimeFormat('en-US').format(date);搜索常见的性能密集型包:
yaml
precision_grep:
queries:
- id: heavy_deps
pattern: "import.*from ['\"](moment|lodash|date-fns|rxjs|@material-ui|antd|chart\\.js)['\"]"
glob: "**/*.{ts,tsx,js,jsx}"
- id: full_library_imports
pattern: "import .* from ['\"](lodash|@mui/material|react-icons)['\"]$"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: locations优化策略:
错误示例 - 导入整个库:
typescript
import _ from 'lodash'; // 71KB gzipped
import * as Icons from 'react-icons/fa'; // 500+ icons正确示例 - 导入特定模块:
typescript
import debounce from 'lodash/debounce'; // 2KB gzipped
import { FaUser, FaCog } from 'react-icons/fa'; // Only what you need更优方案 - 使用现代替代方案:
typescript
// Instead of moment.js (72KB), use date-fns (13KB) or native Intl
import { format } from 'date-fns';
// Or native APIs
const formatted = new Intl.DateTimeFormat('en-US').format(date);Check for Code Splitting
检查代码拆分情况
Find dynamic imports:
yaml
precision_grep:
queries:
- id: dynamic_imports
pattern: "(import\\(|React\\.lazy|next/dynamic)"
glob: "**/*.{ts,tsx,js,jsx}"
- id: large_components
pattern: "export (default )?(function|const).*\\{[\\s\\S]{2000,}"
glob: "src/components/**/*.{tsx,jsx}"
multiline: true
output:
format: files_onlyCode splitting patterns:
Next.js dynamic imports:
typescript
import dynamic from 'next/dynamic';
// Client-only components (no SSR)
const ChartComponent = dynamic(() => import('./Chart'), {
ssr: false,
loading: () => <Spinner />,
});
// Route-based code splitting
const AdminPanel = dynamic(() => import('./AdminPanel'));React.lazy for client components:
typescript
import { lazy, Suspense } from 'react';
const HeavyModal = lazy(() => import('./HeavyModal'));
function App() {
return (
<Suspense fallback={<Spinner />}>
<HeavyModal />
</Suspense>
);
}查找动态导入:
yaml
precision_grep:
queries:
- id: dynamic_imports
pattern: "(import\\(|React\\.lazy|next/dynamic)"
glob: "**/*.{ts,tsx,js,jsx}"
- id: large_components
pattern: "export (default )?(function|const).*\\{[\\s\\S]{2000,}"
glob: "src/components/**/*.{tsx,jsx}"
multiline: true
output:
format: files_only代码拆分模式:
Next.js动态导入:
typescript
import dynamic from 'next/dynamic';
// Client-only components (no SSR)
const ChartComponent = dynamic(() => import('./Chart'), {
ssr: false,
loading: () => <Spinner />,
});
// Route-based code splitting
const AdminPanel = dynamic(() => import('./AdminPanel'));React.lazy用于客户端组件:
typescript
import { lazy, Suspense } from 'react';
const HeavyModal = lazy(() => import('./HeavyModal'));
function App() {
return (
<Suspense fallback={<Spinner />}>
<HeavyModal />
</Suspense>
);
}Phase 3: Database Performance
阶段3:数据库性能
Objective: Eliminate N+1 queries, add missing indexes, and optimize query patterns.
目标: 消除N+1查询、添加缺失索引并优化查询模式。
Detect N+1 Query Patterns
检测N+1查询模式
Search for sequential queries in loops:
yaml
precision_grep:
queries:
- id: potential_n_plus_one
pattern: "(map|forEach).*await.*(findUnique|findFirst|findMany)"
glob: "**/*.{ts,tsx,js,jsx}"
- id: missing_includes
pattern: "findMany\\(\\{[^}]*where[^}]*\\}\\)"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: context
context_before: 3
context_after: 3Common N+1 patterns:
Bad - N+1 query:
typescript
const posts = await db.post.findMany();
// Runs 1 query per post!
const postsWithAuthors = await Promise.all(
posts.map(async (post) => ({
...post,
author: await db.user.findUnique({ where: { id: post.authorId } }),
}))
);Good - eager loading with include:
typescript
const posts = await db.post.findMany({
include: {
author: true,
comments: {
include: {
user: true,
},
},
},
});Better - explicit select for only needed fields:
typescript
const posts = await db.post.findMany({
select: {
id: true,
title: true,
author: {
select: {
id: true,
name: true,
avatar: true,
},
},
},
});搜索循环中的顺序查询:
yaml
precision_grep:
queries:
- id: potential_n_plus_one
pattern: "(map|forEach).*await.*(findUnique|findFirst|findMany)"
glob: "**/*.{ts,tsx,js,jsx}"
- id: missing_includes
pattern: "findMany\\(\\{[^}]*where[^}]*\\}\\)"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: context
context_before: 3
context_after: 3常见N+1模式:
错误示例 - N+1查询:
typescript
const posts = await db.post.findMany();
// Runs 1 query per post!
const postsWithAuthors = await Promise.all(
posts.map(async (post) => ({
...post,
author: await db.user.findUnique({ where: { id: post.authorId } }),
}))
);正确示例 - 使用include预加载:
typescript
const posts = await db.post.findMany({
include: {
author: true,
comments: {
include: {
user: true,
},
},
},
});更优方案 - 显式选择仅需字段:
typescript
const posts = await db.post.findMany({
select: {
id: true,
title: true,
author: {
select: {
id: true,
name: true,
avatar: true,
},
},
},
});Check for Missing Indexes
检查缺失索引
Review Prisma schema for index coverage:
yaml
precision_read:
files:
- path: "prisma/schema.prisma"
extract: content
verbosity: standardThen search for WHERE clauses:
yaml
precision_grep:
queries:
- id: where_clauses
pattern: "where:\\s*\\{\\s*(\\w+):"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: locationsIndex optimization:
Add indexes for frequently queried fields:
prisma
model Post {
id String @id @default(cuid())
title String
published Boolean @default(false)
authorId String
createdAt DateTime @default(now())
// Single column indexes
@@index([authorId])
@@index([createdAt])
// Composite index for common query pattern
@@index([published, createdAt(sort: Desc)])
}When to add indexes:
- Foreign keys used in WHERE clauses or joins
- Fields used in ORDER BY clauses
- Fields used in WHERE with pagination (cursor-based)
- Composite indexes for multi-field queries
When NOT to add indexes:
- Low-cardinality boolean fields (unless part of composite index)
- Fields that change frequently (writes become slower)
- Tables with very few rows
检查Prisma schema的索引覆盖情况:
yaml
precision_read:
files:
- path: "prisma/schema.prisma"
extract: content
verbosity: standard然后搜索WHERE子句:
yaml
precision_grep:
queries:
- id: where_clauses
pattern: "where:\\s*\\{\\s*(\\w+):"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: locations索引优化:
为频繁查询的字段添加索引:
prisma
model Post {
id String @id @default(cuid())
title String
published Boolean @default(false)
authorId String
createdAt DateTime @default(now())
// Single column indexes
@@index([authorId])
@@index([createdAt])
// Composite index for common query pattern
@@index([published, createdAt(sort: Desc)])
}何时添加索引:
- WHERE子句或连接中使用的外键
- ORDER BY子句中使用的字段
- 带分页的WHERE查询中使用的字段(基于游标)
- 多字段查询的复合索引
何时不添加索引:
- 低基数布尔字段(除非是复合索引的一部分)
- 频繁变更的字段(写入速度会变慢)
- 行数极少的表
Optimize Connection Pooling
优化连接池
Check database connection configuration:
yaml
precision_grep:
queries:
- id: prisma_config
pattern: "PrismaClient\\(.*\\{[\\s\\S]*?\\}"
glob: "**/*.{ts,tsx,js,jsx}"
multiline: true
- id: connection_string
pattern: "connection_limit=|pool_timeout=|connect_timeout="
glob: ".env*"
output:
format: context
context_before: 3
context_after: 3Optimal connection pool settings:
typescript
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
});
export { prisma };DATABASE_URL with connection pooling:
bash
undefined检查数据库连接配置:
yaml
precision_grep:
queries:
- id: prisma_config
pattern: "PrismaClient\\(.*\\{[\\s\\S]*?\\}"
glob: "**/*.{ts,tsx,js,jsx}"
multiline: true
- id: connection_string
pattern: "connection_limit=|pool_timeout=|connect_timeout="
glob: ".env*"
output:
format: context
context_before: 3
context_after: 3最优连接池设置:
typescript
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
});
export { prisma };带连接池的DATABASE_URL:
bash
undefinedFor serverless (Vercel, AWS Lambda) - use connection pooler
For serverless (Vercel, AWS Lambda) - use connection pooler
DATABASE_URL="postgresql://user:pass@host:5432/db?connection_limit=10&pool_timeout=10"
DATABASE_URL="postgresql://user:pass@host:5432/db?connection_limit=10&pool_timeout=10"
For long-running servers - higher limits
For long-running servers - higher limits
DATABASE_URL="postgresql://user:pass@host:5432/db?connection_limit=20&pool_timeout=30"
undefinedDATABASE_URL="postgresql://user:pass@host:5432/db?connection_limit=20&pool_timeout=30"
undefinedPhase 4: Rendering Performance
阶段4:渲染性能
Objective: Eliminate unnecessary re-renders and optimize component rendering.
目标: 消除不必要的重渲染并优化组件渲染。
Detect Unnecessary Re-renders
检测不必要的重渲染
Find missing memoization:
Note: These patterns are approximations for single-line detection. Multi-line component definitions may require manual inspection.
yaml
precision_grep:
queries:
- id: missing_memo
pattern: "(const \\w+ = \\{|const \\w+ = \\[|const \\w+ = \\(.*\\) =>)(?!.*useMemo)"
glob: "src/components/**/*.{tsx,jsx}"
- id: missing_callback
pattern: "(onChange|onClick|onSubmit)=\\{.*=>(?!.*useCallback)"
glob: "src/components/**/*.{tsx,jsx}"
- id: memo_usage
pattern: "(useMemo|useCallback|React\\.memo)"
glob: "**/*.{tsx,jsx}"
output:
format: files_onlyMemoization patterns:
Bad - recreates object on every render:
typescript
function UserProfile({ userId }: Props) {
const user = useUser(userId);
// New object reference on every render!
const config = {
showEmail: true,
showPhone: false,
};
return <UserCard user={user} config={config} />;
}Good - memoize stable objects:
typescript
function UserProfile({ userId }: Props) {
const user = useUser(userId);
const config = useMemo(
() => ({
showEmail: true,
showPhone: false,
}),
[] // Empty deps - never changes
);
return <UserCard user={user} config={config} />;
}Memoize callbacks:
typescript
function SearchBar({ onSearch }: Props) {
const [query, setQuery] = useState('');
// Memoize callback to prevent child re-renders
const handleSubmit = useCallback(() => {
onSearch(query);
}, [query, onSearch]);
return (
<form onSubmit={handleSubmit}>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
</form>
);
}Memoize expensive components:
typescript
const ExpensiveChart = React.memo(
function ExpensiveChart({ data }: Props) {
// Heavy computation or rendering
return <Chart data={data} />;
},
(prevProps, nextProps) => {
// Custom comparison - only re-render if data changed
return prevProps.data === nextProps.data;
}
);查找缺失的memoization:
注意: 这些模式是单行检测的近似值。多行组件定义可能需要手动检查。
yaml
precision_grep:
queries:
- id: missing_memo
pattern: "(const \\w+ = \\{|const \\w+ = \\[|const \\w+ = \\(.*\\) =>)(?!.*useMemo)"
glob: "src/components/**/*.{tsx,jsx}"
- id: missing_callback
pattern: "(onChange|onClick|onSubmit)=\\{.*=>(?!.*useCallback)"
glob: "src/components/**/*.{tsx,jsx}"
- id: memo_usage
pattern: "(useMemo|useCallback|React\\.memo)"
glob: "**/*.{tsx,jsx}"
output:
format: files_onlyMemoization模式:
错误示例 - 每次渲染都重新创建对象:
typescript
function UserProfile({ userId }: Props) {
const user = useUser(userId);
// New object reference on every render!
const config = {
showEmail: true,
showPhone: false,
};
return <UserCard user={user} config={config} />;
}正确示例 - 缓存稳定对象:
typescript
function UserProfile({ userId }: Props) {
const user = useUser(userId);
const config = useMemo(
() => ({
showEmail: true,
showPhone: false,
}),
[] // Empty deps - never changes
);
return <UserCard user={user} config={config} />;
}缓存回调函数:
typescript
function SearchBar({ onSearch }: Props) {
const [query, setQuery] = useState('');
// Memoize callback to prevent child re-renders
const handleSubmit = useCallback(() => {
onSearch(query);
}, [query, onSearch]);
return (
<form onSubmit={handleSubmit}>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
</form>
);
}缓存开销大的组件:
typescript
const ExpensiveChart = React.memo(
function ExpensiveChart({ data }: Props) {
// Heavy computation or rendering
return <Chart data={data} />;
},
(prevProps, nextProps) => {
// Custom comparison - only re-render if data changed
return prevProps.data === nextProps.data;
}
);Check for Virtual List Usage
检查虚拟列表使用情况
Find large lists without virtualization:
yaml
precision_grep:
queries:
- id: large_maps
pattern: "\\{.*\\.map\\(.*=>\\s*<(?!Virtualized)(?!VirtualList)"
glob: "src/**/*.{tsx,jsx}"
- id: virtualization
pattern: "(react-window|react-virtualized|@tanstack/react-virtual)"
glob: "package.json"
output:
format: locationsVirtual list implementation:
typescript
import { useVirtualizer } from '@tanstack/react-virtual';
function VirtualList({ items }: { items: Item[] }) {
const parentRef = useRef<HTMLDivElement>(null);
const virtualizer = useVirtualizer({
count: items.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 50, // Estimated row height
overscan: 5, // Render 5 extra rows for smooth scrolling
});
return (
<div ref={parentRef} style={{ height: '600px', overflow: 'auto' }}>
<div
style={{
height: `${virtualizer.getTotalSize()}px`,
position: 'relative',
}}
>
{virtualizer.getVirtualItems().map((virtualRow) => (
<div
key={virtualRow.index}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: `translateY(${virtualRow.start}px)`,
}}
>
<ItemRow item={items[virtualRow.index]} />
</div>
))}
</div>
</div>
);
}查找未使用虚拟化的大型列表:
yaml
precision_grep:
queries:
- id: large_maps
pattern: "\\{.*\\.map\\(.*=>\\s*<(?!Virtualized)(?!VirtualList)"
glob: "src/**/*.{tsx,jsx}"
- id: virtualization
pattern: "(react-window|react-virtualized|@tanstack/react-virtual)"
glob: "package.json"
output:
format: locations虚拟列表实现:
typescript
import { useVirtualizer } from '@tanstack/react-virtual';
function VirtualList({ items }: { items: Item[] }) {
const parentRef = useRef<HTMLDivElement>(null);
const virtualizer = useVirtualizer({
count: items.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 50, // Estimated row height
overscan: 5, // Render 5 extra rows for smooth scrolling
});
return (
<div ref={parentRef} style={{ height: '600px', overflow: 'auto' }}>
<div
style={{
height: `${virtualizer.getTotalSize()}px`,
position: 'relative',
}}
>
{virtualizer.getVirtualItems().map((virtualRow) => (
<div
key={virtualRow.index}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: `translateY(${virtualRow.start}px)`,
}}
>
<ItemRow item={items[virtualRow.index]} />
</div>
))}
</div>
</div>
);
}Phase 5: Network Optimization
阶段5:网络优化
Objective: Reduce request waterfalls, enable caching, and optimize asset delivery.
目标: 减少请求瀑布、启用缓存并优化资源交付。
Detect Request Waterfalls
检测请求瀑布
Find sequential data fetching:
yaml
precision_grep:
queries:
- id: sequential_fetches
pattern: "await fetch.*\\n.*await fetch"
glob: "**/*.{ts,tsx,js,jsx}"
multiline: true
- id: use_effect_fetches
pattern: "useEffect\\(.*fetch"
glob: "**/*.{tsx,jsx}"
output:
format: context
context_before: 3
context_after: 3Waterfall optimization:
Bad - sequential requests:
typescript
async function loadDashboard() {
const userRes = await fetch('/api/user');
if (!userRes.ok) throw new Error(`Failed to fetch user: ${userRes.status}`);
const user = await userRes.json();
const postsRes = await fetch(`/api/posts?userId=${user.id}`);
if (!postsRes.ok) throw new Error(`Failed to fetch posts: ${postsRes.status}`);
const posts = await postsRes.json();
const commentsRes = await fetch(`/api/comments?userId=${user.id}`);
if (!commentsRes.ok) throw new Error(`Failed to fetch comments: ${commentsRes.status}`);
const comments = await commentsRes.json();
return { user, posts, comments };
}Good - parallel requests:
typescript
async function loadDashboard(userId: string) {
const [user, posts, comments] = await Promise.all([
fetch(`/api/user/${userId}`).then(async r => {
if (!r.ok) throw new Error(`Failed to fetch user: ${r.status}`);
return r.json();
}),
fetch(`/api/posts?userId=${userId}`).then(async r => {
if (!r.ok) throw new Error(`Failed to fetch posts: ${r.status}`);
return r.json();
}),
fetch(`/api/comments?userId=${userId}`).then(async r => {
if (!r.ok) throw new Error(`Failed to fetch comments: ${r.status}`);
return r.json();
}),
]);
return { user, posts, comments };
}Best - server-side data aggregation:
typescript
// Single API endpoint that aggregates data server-side
async function loadDashboard(userId: string) {
const response = await fetch(`/api/dashboard?userId=${userId}`);
if (!response.ok) {
throw new Error(`Failed to fetch dashboard: ${response.status}`);
}
const data = await response.json();
return data;
}查找顺序数据获取:
yaml
precision_grep:
queries:
- id: sequential_fetches
pattern: "await fetch.*\\n.*await fetch"
glob: "**/*.{ts,tsx,js,jsx}"
multiline: true
- id: use_effect_fetches
pattern: "useEffect\\(.*fetch"
glob: "**/*.{tsx,jsx}"
output:
format: context
context_before: 3
context_after: 3瀑布优化:
错误示例 - 顺序请求:
typescript
async function loadDashboard() {
const userRes = await fetch('/api/user');
if (!userRes.ok) throw new Error(`Failed to fetch user: ${userRes.status}`);
const user = await userRes.json();
const postsRes = await fetch(`/api/posts?userId=${user.id}`);
if (!postsRes.ok) throw new Error(`Failed to fetch posts: ${postsRes.status}`);
const posts = await postsRes.json();
const commentsRes = await fetch(`/api/comments?userId=${user.id}`);
if (!commentsRes.ok) throw new Error(`Failed to fetch comments: ${commentsRes.status}`);
const comments = await commentsRes.json();
return { user, posts, comments };
}正确示例 - 并行请求:
typescript
async function loadDashboard(userId: string) {
const [user, posts, comments] = await Promise.all([
fetch(`/api/user/${userId}`).then(async r => {
if (!r.ok) throw new Error(`Failed to fetch user: ${r.status}`);
return r.json();
}),
fetch(`/api/posts?userId=${userId}`).then(async r => {
if (!r.ok) throw new Error(`Failed to fetch posts: ${r.status}`);
return r.json();
}),
fetch(`/api/comments?userId=${userId}`).then(async r => {
if (!r.ok) throw new Error(`Failed to fetch comments: ${r.status}`);
return r.json();
}),
]);
return { user, posts, comments };
}最优方案 - 服务器端数据聚合:
typescript
// Single API endpoint that aggregates data server-side
async function loadDashboard(userId: string) {
const response = await fetch(`/api/dashboard?userId=${userId}`);
if (!response.ok) {
throw new Error(`Failed to fetch dashboard: ${response.status}`);
}
const data = await response.json();
return data;
}Check Caching Headers
检查缓存头
Review cache configuration:
yaml
precision_grep:
queries:
- id: cache_headers
pattern: "(Cache-Control|ETag|max-age|stale-while-revalidate)"
glob: "**/*.{ts,tsx,js,jsx}"
- id: next_revalidate
pattern: "(revalidate|force-cache|no-store)"
glob: "src/app/**/*.{ts,tsx}"
output:
format: locationsNext.js caching strategies:
Static data (updates rarely):
typescript
// app/products/page.tsx
export const revalidate = 3600; // Revalidate every hour
export default async function ProductsPage() {
const products = await db.product.findMany();
return <ProductList products={products} />;
}Dynamic data (real-time):
typescript
// app/api/posts/route.ts
export async function GET() {
const posts = await db.post.findMany();
return Response.json(posts, {
headers: {
'Cache-Control': 'private, max-age=60, stale-while-revalidate=300',
},
});
}Opt out of caching:
typescript
// app/api/user/route.ts
export const dynamic = 'force-dynamic';
export async function GET() {
const user = await getCurrentUser();
return Response.json(user);
}检查缓存配置:
yaml
precision_grep:
queries:
- id: cache_headers
pattern: "(Cache-Control|ETag|max-age|stale-while-revalidate)"
glob: "**/*.{ts,tsx,js,jsx}"
- id: next_revalidate
pattern: "(revalidate|force-cache|no-store)"
glob: "src/app/**/*.{ts,tsx}"
output:
format: locationsNext.js缓存策略:
静态数据(很少更新):
typescript
// app/products/page.tsx
export const revalidate = 3600; // Revalidate every hour
export default async function ProductsPage() {
const products = await db.product.findMany();
return <ProductList products={products} />;
}动态数据(实时):
typescript
// app/api/posts/route.ts
export async function GET() {
const posts = await db.post.findMany();
return Response.json(posts, {
headers: {
'Cache-Control': 'private, max-age=60, stale-while-revalidate=300',
},
});
}禁用缓存:
typescript
// app/api/user/route.ts
export const dynamic = 'force-dynamic';
export async function GET() {
const user = await getCurrentUser();
return Response.json(user);
}Optimize Image Loading
优化图片加载
Check for image optimization:
yaml
precision_grep:
queries:
- id: img_tags
pattern: "<img\\s+"
glob: "**/*.{tsx,jsx,html}"
- id: next_image
pattern: "(next/image|Image\\s+from)"
glob: "**/*.{tsx,jsx}"
output:
format: files_onlyImage optimization patterns:
Bad - unoptimized images:
tsx
<img src="/large-photo.jpg" alt="Photo" />Good - Next.js Image component:
tsx
import Image from 'next/image';
<Image
src="/large-photo.jpg"
alt="Photo"
width={800}
height={600}
loading="lazy"
placeholder="blur"
blurDataURL="data:image/jpeg;base64,..."
/>Responsive images:
tsx
<Image
src="/photo.jpg"
alt="Photo"
fill
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
style={{ objectFit: 'cover' }}
/>检查图片优化情况:
yaml
precision_grep:
queries:
- id: img_tags
pattern: "<img\\s+"
glob: "**/*.{tsx,jsx,html}"
- id: next_image
pattern: "(next/image|Image\\s+from)"
glob: "**/*.{tsx,jsx}"
output:
format: files_only图片优化模式:
错误示例 - 未优化的图片:
tsx
<img src="/large-photo.jpg" alt="Photo" />正确示例 - 使用Next.js Image组件:
tsx
import Image from 'next/image';
<Image
src="/large-photo.jpg"
alt="Photo"
width={800}
height={600}
loading="lazy"
placeholder="blur"
blurDataURL="data:image/jpeg;base64,..."
/>响应式图片:
tsx
<Image
src="/photo.jpg"
alt="Photo"
fill
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
style={{ objectFit: 'cover' }}
/>Phase 6: Memory Management
阶段6:内存管理
Objective: Detect memory leaks and optimize garbage collection.
目标: 检测内存泄漏并优化垃圾回收。
Find Memory Leak Patterns
查找内存泄漏模式
Search for cleanup issues:
yaml
precision_grep:
queries:
- id: missing_cleanup
pattern: "useEffect\\(.*\\{[^}]*addEventListener(?!.*return.*removeEventListener)"
glob: "**/*.{tsx,jsx}"
multiline: true
- id: interval_leaks
pattern: "(setInterval|setTimeout)(?!.*clear)"
glob: "**/*.{tsx,jsx}"
- id: subscription_leaks
pattern: "subscribe\\((?!.*unsubscribe)"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: context
context_before: 3
context_after: 3Memory leak patterns:
Bad - event listener leak:
typescript
useEffect(() => {
window.addEventListener('resize', handleResize);
// Missing cleanup!
}, []);Good - proper cleanup:
typescript
useEffect(() => {
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);Bad - interval leak:
typescript
useEffect(() => {
const interval = setInterval(() => {
fetchUpdates();
}, 5000);
// Missing cleanup!
}, []);Good - clear interval:
typescript
useEffect(() => {
const interval = setInterval(() => {
fetchUpdates();
}, 5000);
return () => {
clearInterval(interval);
};
}, []);Bad - fetch without abort:
typescript
useEffect(() => {
fetch('/api/data')
.then(r => r.json() as Promise<DataType>)
.then(setData)
.catch(console.error);
// Missing abort on unmount!
}, []);Good - AbortController cleanup:
typescript
useEffect(() => {
const controller = new AbortController();
fetch('/api/data', { signal: controller.signal })
.then(r => r.json() as Promise<DataType>)
.then(setData)
.catch(err => {
if (err.name !== 'AbortError') console.error(err);
});
return () => controller.abort();
}, []);WeakRef pattern for caches:
typescript
class ImageCache {
private cache = new Map<string, WeakRef<HTMLImageElement>>();
get(url: string): HTMLImageElement | undefined {
const ref = this.cache.get(url);
return ref?.deref();
}
set(url: string, img: HTMLImageElement): void {
this.cache.set(url, new WeakRef(img));
}
}搜索清理问题:
yaml
precision_grep:
queries:
- id: missing_cleanup
pattern: "useEffect\\(.*\\{[^}]*addEventListener(?!.*return.*removeEventListener)"
glob: "**/*.{tsx,jsx}"
multiline: true
- id: interval_leaks
pattern: "(setInterval|setTimeout)(?!.*clear)"
glob: "**/*.{tsx,jsx}"
- id: subscription_leaks
pattern: "subscribe\\((?!.*unsubscribe)"
glob: "**/*.{ts,tsx,js,jsx}"
output:
format: context
context_before: 3
context_after: 3内存泄漏模式:
错误示例 - 事件监听器泄漏:
typescript
useEffect(() => {
window.addEventListener('resize', handleResize);
// Missing cleanup!
}, []);正确示例 - 正确清理:
typescript
useEffect(() => {
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);错误示例 - 定时器泄漏:
typescript
useEffect(() => {
const interval = setInterval(() => {
fetchUpdates();
}, 5000);
// Missing cleanup!
}, []);正确示例 - 清除定时器:
typescript
useEffect(() => {
const interval = setInterval(() => {
fetchUpdates();
}, 5000);
return () => {
clearInterval(interval);
};
}, []);错误示例 - 未终止的fetch请求:
typescript
useEffect(() => {
fetch('/api/data')
.then(r => r.json() as Promise<DataType>)
.then(setData)
.catch(console.error);
// Missing abort on unmount!
}, []);正确示例 - 使用AbortController清理:
typescript
useEffect(() => {
const controller = new AbortController();
fetch('/api/data', { signal: controller.signal })
.then(r => r.json() as Promise<DataType>)
.then(setData)
.catch(err => {
if (err.name !== 'AbortError') console.error(err);
});
return () => controller.abort();
}, []);缓存的WeakRef模式:
typescript
class ImageCache {
private cache = new Map<string, WeakRef<HTMLImageElement>>();
get(url: string): HTMLImageElement | undefined {
const ref = this.cache.get(url);
return ref?.deref();
}
set(url: string, img: HTMLImageElement): void {
this.cache.set(url, new WeakRef(img));
}
}Phase 7: Server-Side Performance
阶段7:服务器端性能
Objective: Optimize SSR, streaming, and edge function performance.
目标: 优化SSR、流式传输和边缘函数性能。
Check for Blocking Server Components
检查阻塞性服务器组件
Find slow server components:
yaml
precision_grep:
queries:
- id: server_components
pattern: "export default async function.*Page"
glob: "src/app/**/page.{tsx,jsx}"
- id: blocking_awaits
pattern: "const.*await.*\\n.*const.*await"
glob: "src/app/**/*.{tsx,jsx}"
multiline: true
output:
format: locationsStreaming with Suspense:
Bad - blocking entire page:
typescript
// app/dashboard/page.tsx
export default async function DashboardPage() {
const user = await getUser();
const posts = await getPosts(); // Blocks entire page!
const analytics = await getAnalytics(); // Blocks entire page!
return (
<div>
<UserProfile user={user} />
<PostList posts={posts} />
<Analytics data={analytics} />
</div>
);
}Good - streaming with Suspense:
typescript
// app/dashboard/page.tsx
import { Suspense } from 'react';
export default async function DashboardPage() {
// Only block on critical data
const user = await getUser();
return (
<div>
<UserProfile user={user} />
<Suspense fallback={<PostListSkeleton />}>
<PostList />
</Suspense>
<Suspense fallback={<AnalyticsSkeleton />}>
<Analytics />
</Suspense>
</div>
);
}
// Separate component for async data
async function PostList() {
const posts = await getPosts();
return <div>{/* render posts */}</div>;
}查找慢服务器组件:
yaml
precision_grep:
queries:
- id: server_components
pattern: "export default async function.*Page"
glob: "src/app/**/page.{tsx,jsx}"
- id: blocking_awaits
pattern: "const.*await.*\\n.*const.*await"
glob: "src/app/**/*.{tsx,jsx}"
multiline: true
output:
format: locations使用Suspense流式传输:
错误示例 - 阻塞整个页面:
typescript
// app/dashboard/page.tsx
export default async function DashboardPage() {
const user = await getUser();
const posts = await getPosts(); // Blocks entire page!
const analytics = await getAnalytics(); // Blocks entire page!
return (
<div>
<UserProfile user={user} />
<PostList posts={posts} />
<Analytics data={analytics} />
</div>
);
}正确示例 - 使用Suspense流式传输:
typescript
// app/dashboard/page.tsx
import { Suspense } from 'react';
export default async function DashboardPage() {
// Only block on critical data
const user = await getUser();
return (
<div>
<UserProfile user={user} />
<Suspense fallback={<PostListSkeleton />}>
<PostList />
</Suspense>
<Suspense fallback={<AnalyticsSkeleton />}>
<Analytics />
</Suspense>
</div>
);
}
// Separate component for async data
async function PostList() {
const posts = await getPosts();
return <div>{/* render posts */}</div>;
}Optimize Edge Functions
优化边缘函数
Check edge runtime usage:
yaml
precision_grep:
queries:
- id: edge_config
pattern: "export const runtime = ['\"](edge|nodejs)['\"]"
glob: "**/*.{ts,tsx}"
- id: edge_incompatible
pattern: "(fs\\.|path\\.|process\\.cwd)"
glob: "**/api/**/*.{ts,tsx}"
output:
format: locationsEdge runtime best practices:
typescript
// app/api/geo/route.ts
export const runtime = 'edge';
export async function GET(request: Request) {
// Access edge-specific APIs
const geo = request.headers.get('x-vercel-ip-country');
return Response.json({
country: geo,
timestamp: Date.now(),
});
}检查边缘运行时使用情况:
yaml
precision_grep:
queries:
- id: edge_config
pattern: "export const runtime = ['\"](edge|nodejs)['\"]"
glob: "**/*.{ts,tsx}"
- id: edge_incompatible
pattern: "(fs\\.|path\\.|process\\.cwd)"
glob: "**/api/**/*.{ts,tsx}"
output:
format: locations边缘运行时最佳实践:
typescript
// app/api/geo/route.ts
export const runtime = 'edge';
export async function GET(request: Request) {
// Access edge-specific APIs
const geo = request.headers.get('x-vercel-ip-country');
return Response.json({
country: geo,
timestamp: Date.now(),
});
}Phase 8: Core Web Vitals
阶段8:Core Web Vitals
Objective: Measure and optimize LCP, INP, and CLS.
目标: 测量并优化LCP、INP和CLS。
Measure Web Vitals
测量Web Vitals
Add Web Vitals reporting:
typescript
// app/layout.tsx or app/web-vitals.tsx
'use client';
import { useEffect } from 'react';
import { onCLS, onFCP, onINP, onLCP, onTTFB } from 'web-vitals';
import type { Metric } from 'web-vitals';
function sendToAnalytics(metric: Metric) {
const body = JSON.stringify(metric);
const url = '/api/analytics';
if (navigator.sendBeacon) {
navigator.sendBeacon(url, body);
} else {
fetch(url, { body, method: 'POST', keepalive: true });
}
}
export function WebVitals() {
useEffect(() => {
onCLS(sendToAnalytics);
onFCP(sendToAnalytics);
onINP(sendToAnalytics);
onLCP(sendToAnalytics);
onTTFB(sendToAnalytics);
}, []);
return null;
}添加Web Vitals上报:
typescript
// app/layout.tsx or app/web-vitals.tsx
'use client';
import { useEffect } from 'react';
import { onCLS, onFCP, onINP, onLCP, onTTFB } from 'web-vitals';
import type { Metric } from 'web-vitals';
function sendToAnalytics(metric: Metric) {
const body = JSON.stringify(metric);
const url = '/api/analytics';
if (navigator.sendBeacon) {
navigator.sendBeacon(url, body);
} else {
fetch(url, { body, method: 'POST', keepalive: true });
}
}
export function WebVitals() {
useEffect(() => {
onCLS(sendToAnalytics);
onFCP(sendToAnalytics);
onINP(sendToAnalytics);
onLCP(sendToAnalytics);
onTTFB(sendToAnalytics);
}, []);
return null;
}Optimize LCP (Largest Contentful Paint)
优化LCP(最大内容绘制)
Target: < 2.5s
Common LCP issues:
- Large hero images not optimized
- Web fonts blocking render
- Server-side rendering too slow
- No resource prioritization
Optimization:
tsx
// Priority image loading
<Image
src="/hero.jpg"
alt="Hero"
width={1200}
height={630}
priority // Preload this image!
/>
// Font optimization
<link
rel="preload"
href="/fonts/inter.woff2"
as="font"
type="font/woff2"
crossOrigin="anonymous"
/>目标:< 2.5秒
常见LCP问题:
- 大型首屏图片未优化
- Web字体阻塞渲染
- 服务器端渲染过慢
- 没有资源优先级设置
优化方案:
tsx
// Priority image loading
<Image
src="/hero.jpg"
alt="Hero"
width={1200}
height={630}
priority // Preload this image!
/>
// Font optimization
<link
rel="preload"
href="/fonts/inter.woff2"
as="font"
type="font/woff2"
crossOrigin="anonymous"
/>Optimize INP (Interaction to Next Paint)
优化INP(交互到下一次绘制)
Target: < 200ms
Common INP issues:
- Heavy JavaScript blocking main thread
- Expensive event handlers
- Layout thrashing
Optimization:
typescript
// Debounce expensive handlers
import { useDebouncedCallback } from 'use-debounce';
function SearchInput() {
const debouncedSearch = useDebouncedCallback(
(value: string) => {
performSearch(value);
},
300 // Wait 300ms after user stops typing
);
return (
<input
onChange={(e) => debouncedSearch(e.target.value)}
placeholder="Search..."
/>
);
}目标:< 200毫秒
常见INP问题:
- 繁重的JavaScript阻塞主线程
- 昂贵的事件处理程序
- 布局抖动
优化方案:
typescript
// Debounce expensive handlers
import { useDebouncedCallback } from 'use-debounce';
function SearchInput() {
const debouncedSearch = useDebouncedCallback(
(value: string) => {
performSearch(value);
},
300 // Wait 300ms after user stops typing
);
return (
<input
onChange={(e) => debouncedSearch(e.target.value)}
placeholder="Search..."
/>
);
}Optimize CLS (Cumulative Layout Shift)
优化CLS(累积布局偏移)
Target: < 0.1
Common CLS issues:
- Images without dimensions
- Ads or embeds without reserved space
- Web fonts causing FOIT/FOUT
- Dynamic content injection
Optimization:
tsx
// Always specify image dimensions
<Image
src="/photo.jpg"
alt="Photo"
width={800}
height={600}
/>
// Reserve space for dynamic content
<div style={{ minHeight: '200px' }}>
<Suspense fallback={<Skeleton height={200} />}>
<DynamicContent />
</Suspense>
</div>
// Use font-display for web fonts
@font-face {
font-family: 'Inter';
src: url('/fonts/inter.woff2') format('woff2');
font-display: swap; // Prevent invisible text
}目标:< 0.1
常见CLS问题:
- 图片未指定尺寸
- 广告或嵌入内容未预留空间
- Web字体导致FOIT/FOUT
- 动态内容注入
优化方案:
tsx
// Always specify image dimensions
<Image
src="/photo.jpg"
alt="Photo"
width={800}
height={600}
/>
// Reserve space for dynamic content
<div style={{ minHeight: '200px' }}>
<Suspense fallback={<Skeleton height={200} />}>
<DynamicContent />
</Suspense>
</div>
// Use font-display for web fonts
@font-face {
font-family: 'Inter';
src: url('/fonts/inter.woff2') format('woff2');
font-display: swap; // Prevent invisible text
}Audit Reporting
审计报告
Structure findings with impact, effort, and priority:
按影响程度、修复成本和优先级整理发现的问题:
Report Template
报告模板
markdown
undefinedmarkdown
undefinedPerformance Audit Report
性能审计报告
Executive Summary
执行摘要
- Date: 2026-02-16
- Auditor: Engineer Agent
- Scope: Full application performance review
- Overall Score: 6.5/10
- 日期: 2026-02-16
- 审计者: 工程师Agent
- 范围: 全应用性能评审
- 总体得分: 6.5/10
Critical Issues (Fix Immediately)
关键问题(立即修复)
1. N+1 Query in Post Listing
1. 文章列表中的N+1查询
- File:
src/app/posts/page.tsx - Issue: Sequential database queries for each post author
- Impact: Page load time 3.2s -> should be <500ms
- Effort: 15 minutes
- Fix: Add to findMany query
include: { author: true }
- 文件:
src/app/posts/page.tsx - 问题: 为每篇文章的作者执行顺序数据库查询
- 影响: 页面加载时间3.2秒 -> 应小于500毫秒
- 修复成本: 15分钟
- 修复方案: 在findMany查询中添加
include: { author: true }
High Priority (Fix This Week)
高优先级(本周修复)
2. Missing Bundle Splitting
2. 缺失的代码拆分
- Files:
src/app/admin/* - Issue: Admin panel code (250KB) loaded on all pages
- Impact: Initial bundle size 850KB -> should be <200KB
- Effort: 1 hour
- Fix: Use for admin routes
next/dynamic
- 文件:
src/app/admin/* - 问题: 管理面板代码(250KB)在所有页面加载
- 影响: 初始打包体积850KB -> 应小于200KB
- 修复成本: 1小时
- 修复方案: 对管理路由使用
next/dynamic
Medium Priority (Fix This Month)
中优先级(本月修复)
3. Unoptimized Images
3. 未优化的图片
- Files: Multiple components using tags
<img> - Issue: LCP of 4.1s due to large unoptimized images
- Impact: Poor Core Web Vitals, SEO penalty
- Effort: 2 hours
- Fix: Migrate to with proper sizing
next/image
- 文件: 多个使用标签的组件
<img> - 问题: 大型未优化图片导致LCP为4.1秒
- 影响: Core Web Vitals表现差,SEO受罚
- 修复成本: 2小时
- 修复方案: 迁移到并设置合适尺寸
next/image
Low Priority (Backlog)
低优先级(待办)
4. Missing Memoization
4. 缺失的Memoization
- Files:
src/components/Dashboard/*.tsx - Issue: Unnecessary re-renders on state changes
- Impact: Minor UI lag on interactions
- Effort: 3 hours
- Fix: Add ,
useMemo,useCallbackReact.memo
- 文件:
src/components/Dashboard/*.tsx - 问题: 状态变化时不必要的重渲染
- 影响: 交互时轻微UI卡顿
- 修复成本: 3小时
- 修复方案: 添加、
useMemo、useCallbackReact.memo
Performance Metrics
性能指标
| Metric | Current | Target | Status |
|---|---|---|---|
| LCP | 4.1s | <2.5s | FAIL |
| INP | 180ms | <200ms | PASS |
| CLS | 0.05 | <0.1 | PASS |
| Bundle Size | 850KB | <200KB | FAIL |
| API Response | 320ms | <500ms | PASS |
undefined| 指标 | 当前值 | 目标值 | 状态 |
|---|---|---|---|
| LCP | 4.1秒 | <2.5秒 | 失败 |
| INP | 180毫秒 | <200毫秒 | 通过 |
| CLS | 0.05 | <0.1 | 通过 |
| 打包体积 | 850KB | <200KB | 失败 |
| API响应时间 | 320毫秒 | <500毫秒 | 通过 |
undefinedValidation Script
验证脚本
Use the validation script to verify audit completeness:
bash
bash scripts/validate-performance-audit.shThe script checks for:
- Bundle analysis artifacts
- Database query patterns
- Memoization usage
- Image optimization
- Caching headers
使用验证脚本确认审计完整性:
bash
bash scripts/validate-performance-audit.sh脚本检查以下内容:
- 打包分析产物
- 数据库查询模式
- Memoization使用情况
- 图片优化
- 缓存头
References
参考资料
See for detailed anti-patterns and optimization techniques.
references/performance-patterns.md详细的反模式和优化技术请参见。
references/performance-patterns.md