Communitygithub.com

性能剖析器

识别 N+1 查询、低效循环、内存泄漏和缓慢算法等常见性能瓶颈,并给出修复建议。在用户提及性能问题、代码缓慢、优化或性能分析时使用。

性能剖析器 是什么?

该技能旨在帮助开发者和 AI 代理识别并解决代码中的性能瓶颈。它覆盖了全面的性能优化领域,包括 N+1 查询、低效循环(如 O(n²) 复杂度)、不必要的 React 重新渲染、内存泄漏、阻塞操作、数据库查询效率低下、算法复杂度问题、前端性能(如 bundle 大小、图片优化)以及网络性能。技能提供了每种问题的具体代码示例(反模式和正例),以及如何改用哈希表、索引优化、懒加载、缓存等技术进行修复。此外,它还包含生成结构化性能报告的能力,报告按优先级(严重、高、中)列出问题、影响和修复建议,并推荐了 JavaScript (Chrome DevTools, Node.js --inspect)、Python (cProfile, line_profiler) 和数据库 (EXPLAIN, 慢查询日志) 的 profiling 工具。该技能适用于任何需要优化代码性能的上下文,例如 Claude Code、Cursor 或 Codex 等 AI 编程助手在执行代码审查、调试或性能优化任务时。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComeOnOliver/skillshub/tree/HEAD/skills/aiskillstore/marketplace/crazydubya/performance-profiler

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

性能剖析器 是做什么的?

识别并建议常见代码性能瓶颈的修复方案。

何时使用

  • 用户报告性能问题或代码缓慢
  • 优化请求
  • 代码审查中的性能检查
  • 用户提及“慢”、“瓶颈”、“优化”、“内存泄漏”

指令

1. 识别性能反模式

N+1 查询问题:

// 坏:N+1 查询
users.forEach(user => {
  const posts = db.query('SELECT * FROM posts WHERE user_id = ?', user.id);
});

// 好:用 JOIN 的单次查询
const usersWithPosts = db.query('SELECT * FROM users LEFT JOIN posts ON users.id = posts.user_id');

低效循环:

# 坏:O(n²) 嵌套循环
for item in list1:
    for other in list2:
        if item.id == other.id:
            process(item, other)

# 好:O(n) 使用哈希表
lookup = {other.id: other for other in list2}
for item in list1:
    if item.id in lookup:
        process(item, lookup[item.id])

不必要的重渲染 (React):

// 坏:每次渲染都创建新对象
<Component style={{ margin: 10 }} />

// 好:在外部定义或使用 useMemo
const style = { margin: 10 };
<Component style={style} />

内存泄漏:

  • 事件监听未清理
  • 定时器未清除
  • 循环引用
  • 无限制的大缓存

阻塞操作:

  • 同步文件 I/O
  • UI 线程中的长时计算
  • 缺少分页

2. 数据库性能

检查:

  • 外键上缺少索引
  • SELECT * 而非具体列
  • 循环中的查询 (N+1)
  • 缺少查询限制
  • 低效的 JOIN

建议:

  • 添加索引:CREATE INDEX idx_user_id ON posts(user_id);
  • 使用预加载/预取
  • 实现分页
  • 使用数据库查询分析器 (EXPLAIN)

3. 算法复杂度

识别:

  • O(n²) 或更差的算法
  • 重复计算
  • 不必要的排序
  • 低效的数据结构

常见修复:

  • 哈希表用于 O(1) 查找 替代 O(n) 数组搜索
  • 二分查找 O(log n) 替代 线性查找 O(n)
  • 记忆化用于重复计算
  • 惰性求值用于耗时操作

4. 前端性能

检查:

  • 打包体积过大
  • 未优化的图片
  • 缺少代码分割
  • 低效的 React 组件
  • 缺少记忆化

建议:

  • 懒加载:const Component = lazy(() => import('./Component'));
  • 图片优化
  • 防抖/节流耗时操作
  • 长列表虚拟滚动
  • Web Workers 处理繁重计算

5. 网络性能

问题:

  • HTTP 请求过多
  • 负载过大
  • 缺少缓存
  • 同步请求

解决方案:

  • 合并/串联资源
  • 实施压缩 (gzip, brotli)
  • 使用 HTTP/2 多路复用
  • 添加缓存头
  • 并行请求 替代 串行请求

6. 生成性能报告

性能分析
===================

严重问题(立即修复):
1. UserController.index 中的 N+1 查询 (file.js:45)
   - 影响:每次请求 100+ 次数据库查询
   - 修复:使用预加载或 JOIN

2. EventEmitter 中的内存泄漏 (file.js:120)
   - 影响:内存无限增长
   - 修复:在清理时移除监听器

高优先级:
3. processData 中的 O(n²) 循环 (file.js:200)
   - 影响:大数据集时缓慢
   - 修复:使用哈希表达到 O(n)

中优先级:
4. 缺少图片优化
   - 影响:页面加载缓慢
   - 修复:使用 next/image 或手动优化

7. 性能分析工具

JavaScript:

  • Chrome DevTools 性能面板
  • Node.js --inspect 标志
  • console.time() / console.timeEnd()

Python:

  • cProfile 模块
  • line_profiler
  • memory_profiler

数据库:

  • EXPLAIN / EXPLAIN ANALYZE
  • 慢查询日志
  • pg_stat_statements (PostgreSQL)

最佳实践

  • 先分析再优化
  • 关注热点路径 (80/20 法则)
  • 衡量更改的影响
  • 权衡可读性与性能
  • 记录性能关键部分

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

ComeOnOliver/skillshub

Create stunning, animation-rich HTML presentations from scratch or by converting PowerPoint files. Use when the user wants to build a presentation, convert a PPT/PPTX to web, or create slides for a talk/pitch. Helps non-designers discover their aesthetic through visual exploration rather than abstract choices.

ComeOnOliver/skillshub

Next.js 16+ 和 Turbopack — 增量打包、文件系统缓存、开发速度,以及何时使用 Turbopack 与 webpack。

ComeOnOliver/skillshub

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

ComeOnOliver/skillshub

See, Understand, Act on video and audio. See- ingest from local files, URLs, RTSP/live feeds, or live record desktop; return realtime context and playable stream links. Understand- extract frames, build visual/semantic/temporal indexes, and search moments with timestamps and auto-clips. Act- transcode and normalize (codec, fps, resolution, aspect ratio), perform timeline edits (subtitles, text/image overlays, branding, audio overlays, dubbing, translation), generate media assets (image, audio, video), and create real time alerts for events from live streams or desktop capture.

ComeOnOliver/skillshub

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

ComeOnOliver/skillshub

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

ComeOnOliver/skillshub

Provides Tamagui patterns for config v4, compiler optimization, styled context, and cross-platform styling. Must use when working with Tamagui projects (tamagui.config.ts, @tamagui imports).

ComeOnOliver/skillshub

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

ComeOnOliver/skillshub

Create high-converting, visually distinctive landing pages. Use when building marketing pages, product launches, SaaS homepages, or any single-page conversion-focused website. Guides section-by-section composition with anti-AI-slop principles.

ComeOnOliver/skillshub

Auto-loaded context for Portfolio Buddy 2 development. Use for ANY task involving: React 19 development, TypeScript, portfolio analysis features, metrics calculations, trading strategy comparison, or working with the Portfolio Buddy 2 codebase. Contains tech stack, known issues, and architectural constraints.

ComeOnOliver/skillshub

Next.js development tooling via MCP. Inspect routes, components, build info, and debug Next.js apps. Use when working on Next.js applications, debugging routing, or inspecting app structure. NOT for general React or non-Next.js projects.

ComeOnOliver/skillshub

Implement UI using Shadcn MCP (atoms/theme) and 21st.dev MCP (complex sections). Use when adding buttons, layouts, or generating landing pages.

ComeOnOliver/skillshub

A conceptual skill for building an API client in Next.js that handles JWT tokens

ComeOnOliver/skillshub

Best practices and patterns for Next.js App Router, Server Actions, and Routing in this project.

ComeOnOliver/skillshub

The Design System, Theme, and UX rules for the Physical AI Hub.

ComeOnOliver/skillshub

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

ComeOnOliver/skillshub

This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations.

ComeOnOliver/skillshub

Build Next.js 16 applications with correct patterns and distinctive design. Use when creating pages, layouts, dynamic routes, upgrading from Next.js 15, or implementing proxy.ts. Covers breaking changes (async params/searchParams, Turbopack, cacheComponents) and frontend aesthetics. NOT when building non-React or backend-only applications.

ComeOnOliver/skillshub

Comprehensive Framer Motion animation library for React. Covers motion components, variants, gestures, page transitions, and scroll animations. Use when adding animations to React/Next.js applications.

ComeOnOliver/skillshub

Build interfaces that resonate, not just render. This skill enforces bold, intentional

相关技能