Community编程与开发github.com

alfathafp/portofolio-web-design

优化前端、后端、查询和数据库的应用程序性能。适用于存在性能需求、怀疑性能回归、需要改进核心网页指标或加载时间、修复 N+1 查询模式或分析揭示瓶颈时。

portofolio-web-design 是什么?

该技能为开发者和 AI 代理提供了一套结构化的性能优化方法论,强调“先测量,再优化”的原则。它详细介绍了从建立基线、识别瓶颈、实施修复、验证效果到防止回归的完整工作流。技能支持跨平台(前端、后端、查询和数据库),并提供了针对核心网页指标(LCP、INP、CLS)的明确目标和优化模式。文档包含了针对 N+1 查询、未优化的图片、大型 bundle 等常见反模式的识别特征和修复指南,并强调了通过持续测量和回归测试来确保优化有效性的重要性。该技能适用于需要处理性能要求的任何环境,如 Claude Code、Cursor 或 Codex。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/alfathafp/portofolio-web-design/tree/HEAD/.agents/skills/performance-optimization

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

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

文档

性能优化

概述

先测量,再优化。没有测量的性能工作只是猜测——而猜测会导致过早优化,增加复杂性却没有改善真正重要的事情。先进行分析,找出实际的瓶颈,修复它,再测量一次。只优化那些测量证明是重要的部分。

何时使用

  • 规格中存在性能要求(加载时间预算、响应时间 SLA)
  • 用户或监控报告行为缓慢
  • 核心网页指标分数低于阈值
  • 您怀疑某个更改引入了回归
  • 构建处理大数据集或高流量的功能

何时不使用: 在您有证据表明存在性能问题之前,不要进行优化。过早优化会增加复杂性,其成本超过所获得的性能收益。

核心网页指标目标

指标良好需要改进较差
LCP (最大内容绘制)≤ 2.5s≤ 4.0s> 4.0s
INP (交互到下一次绘制)≤ 200ms≤ 500ms> 500ms
CLS (累积布局偏移)≤ 0.1≤ 0.25> 0.25

优化工作流

1. 测量  → 使用真实数据建立基线
2. 识别  → 找到实际的瓶颈(而不是假设的)
3. 修复   → 解决特定瓶颈
4. 验证   → 再次测量;保留或回滚
5. 防护   → 添加监控或测试以防止回归

步骤 1:测量

两种互补的方法——同时使用:

  • 合成测试 (Lighthouse, DevTools 性能选项卡): 受控条件,可重现。最适合 CI 回归检测和隔离特定问题。
  • RUM (web-vitals 库, CrUX): 真实条件下的真实用户数据。需要验证修复是否真正改善了用户体验。

前端:

# 合成测试:Chrome DevTools 中的 Lighthouse(或 CI)
# Chrome DevTools → 性能选项卡 → 记录
# Chrome DevTools MCP → 性能跟踪

# RUM:代码中的 Web Vitals 库
import { onLCP, onINP, onCLS } from 'web-vitals';

onLCP(console.log);
onINP(console.log);
onCLS(console.log);

后端:

# 响应时间日志记录
# 应用程序性能监控 (APM)
# 带计时的数据库查询日志

# 简单计时
console.time('db-query');
const result = await db.query(...);
console.timeEnd('db-query');

从何处开始测量

根据症状决定首先测量什么:

什么速度慢?
├── 首次页面加载
│   ├── 包过大?--> 测量包大小,检查代码分割
│   ├── 服务器响应慢?--> 在 DevTools 网络瀑布流中测量 TTFB
│   │   ├── DNS 长?--> 为已知来源添加 dns-prefetch / preconnect
│   │   ├── TCP/TLS 长?--> 启用 HTTP/2,检查边缘部署,启用 keep-alive
│   │   └── 等待(服务器)时间长?--> 分析后端,检查查询和缓存
│   └── 渲染阻塞资源?--> 检查网络瀑布流中阻塞的 CSS/JS
├── 交互感觉迟缓
│   ├── 点击时 UI 冻结?--> 分析主线程,查找长任务(>50ms)
│   ├── 表单输入延迟?--> 检查重渲染,受控组件开销
│   └── 动画卡顿?--> 检查布局抖动、强制重排
├── 导航后的页面
│   ├── 数据加载?--> 测量 API 响应时间,检查瀑布流
│   └── 客户端渲染?--> 分析组件渲染时间,检查 N+1 次获取
└── 后端 / API
    ├── 单个端点慢?--> 分析数据库查询,检查索引
    ├── 所有端点慢?--> 检查连接池、内存、CPU
    └── 间歇性缓慢?--> 检查锁争用、GC 暂停、外部依赖

步骤 2:识别瓶颈

按类别划分的常见瓶颈:

前端:

症状可能原因调查方法
LCP 慢大图像、渲染阻塞资源、服务器慢检查网络瀑布流、图像大小
CLS 高没有尺寸的图像、后期加载的内容、字体闪烁检查布局偏移归因
INP 差主线程上的繁重 JavaScript、大型 DOM 更新在性能跟踪中检查长任务
初始加载慢大包、许多网络请求检查包大小、代码分割

后端:

症状可能原因调查方法
API 响应慢N+1 查询、缺少索引、未优化的查询检查数据库查询日志
内存增长泄漏的引用、无界缓存、大负载堆快照分析
CPU 峰值同步繁重计算、正则表达式回溯CPU 性能分析
高延迟缺少缓存、冗余计算、网络跳转在栈中跟踪请求

步骤 3:修复瓶颈

只修复步骤 2 识别出的那一个问题,不要做其他任何事情。以下的反模式是常见的嫌疑对象;每个条目都给出了规则和识别它的特征,并链接到 references/optimization-patterns.md 中的已修复示例。仅在您遇到该代码时才打开您需要的那个,而不是在此之前。

后端

  • N+1 查询。 每行一个查询是最常见的后端瓶颈。在同一查询(join/include)中获取关联,而不是在循环中获取。模式。
  • 无界数据获取。 每个列表端点都使用限制和稳定顺序进行分页。模式。
  • 忽略索引的查询。 "添加索引"是猜测;EXPLAIN ANALYZE 是测量。在您期望索引的位置出现Seq Scan,rows=估计偏离一个数量级,以及扫描上方的Sort节点,每个都需要不同的修复;错误的rows=估计意味着统计信息过时,因此运行ANALYZE而不是添加索引。为查询的形状建立索引(首先使用等值列,然后使用范围或排序列)。对于低选择性的主导值(部分索引服务于稀有值)、前导通配符(需要 trigram 或全文搜索)或应用于列的函数(对表达式建立索引,如WHERE lower(email) = ?),普通索引将无济于事,并且每个索引都会增加每次写入的开销。之后重新运行计划;没有改变任何内容的索引应该回滚。模式。
  • 连接池耗尽。 特征:每个端点同时变慢,时间花在等待连接而不是执行,并且数据库显示大部分空闲会话。每个进程一个连接池,大小设置为实例数 × 最大值保持在数据库的连接上限以下。连接池更大并不更快;它只是将队列重新定位到数据库中,在那里更难看到。对于无界实例数(无服务器、自动伸缩),通过代理(pgbouncer、RDS Proxy)进行多路复用,而不是提高最大值。模式。
  • 缺少缓存。 缓存那些生成成本高且读取频率远高于更改频率的内容;缓存一个已经很快的查询增加了网络跳转和过期错误,却没有任何回报。有意识地选择缓存层(进程内、共享、CDN)。所有影响响应的输入都应包含在键中(租户、区域设置、权限、功能标志):省略查看者的键会导致一个用户的数据被提供给另一个用户。选择一种失效策略(TTL、基于事件或标签、版本化键)并明确说明可接受的过期窗口。防止热键的冲击:在单个请求重新计算时提供过期数据,或者将并发未命中合并到单个正在进行的 promise 之后。永远不要缓存必须是新鲜的数据(余额、权限、结账时的库存)。模式;请求合并、写入策略和负面缓存在../../references/performance-checklist.md中。

前端

  • 缺少图像优化。 每个图像声明width和height(CLS)。LCP 图像使用fetchpriority="high",通过<picture>使用现代格式(AVIF、WebP),并使用srcset/sizes进行分辨率切换;折叠线以下的图像使用loading="lazy"和decoding="async"。模式。
  • 不必要的重渲染。 在渲染中创建的对象或函数每次都是新的引用,并会重新渲染接收它的每个子组件。提升常量;将React.memo和useMemo保留用于分析显示成本高昂的工作,因为过度使用本身就是一种成本。模式。
  • 包大小过大。 现代打包器会自动删除 ESM 具名导入中的死代码;真正的收益在于路由级代码分割和惰性加载重型、不常用的功能到Suspense后面。在更改导入样式之前进行分析。模式。

步骤 4:验证(保留或回滚)

修复在您重新测量之前只是一个假设。此步骤决定它是否保留。

以测量基线的方式重新测量: 相同的命令、相同的条件、相同的固定预算(挂钟时间、样本数或请求数)。在冷缓存上获取的基线与在热缓存上获取的结果进行对比,测量的是缓存,而不是您的更改。

一次只更改一件事。 三个优化一起落地会产生一个数字,您无法归因于任何一个。如果它们必须一起发布,请先分别测量每个优化。

打败噪音,而不仅仅是平均值。 重复测量并将增量与运行间方差进行比较。在 ±5% 的方差内获得 3% 的增益并不是一个收益;它只是一个不同的样本。

然后严格决定:

相对于基线的结果操作
超过阈值,测试通过保留。 在提交信息中提交前后数据。
在噪音范围内(无可测量的变化)回滚。
更差回滚。
改进,但测试失败回滚。 一个伪装成胜利的回归。

“中性”是回滚,而不是保留。 这是团队跳过的一步:更改已经写好了,丢弃它感觉浪费,所以它在未测量的情况下被合并,代码库积累了从未带来任何好处的复杂性。您保留的代码,您需要永远维护它。让它为自己付出代价。

正确性优先于指标。 测试套件保持绿色并且数字有所改善。一个通过放弃产品所需的工作(跳过验证、缓存必须是新鲜的内容、删除负载均衡的await)而获胜的“优化”是一种回归,而不是胜利。

记录每次尝试,包括那些回滚的

回滚的工作在 git 历史中没有留下痕迹,这正是为什么同一死胡同的想法会在下个季度再次被尝试。保存一个简短的日志,以便被丢弃的想法保持被丢弃的状态:

想法基线 → 结果判决原因
记忆化行组件INP 240ms → 235ms回滚在噪音范围内(±15ms)。行不是瓶颈。
虚拟化列表INP 240ms → 90ms保留跟踪中长任务消失。
预连接到 API 源LCP 2.8s → 2.8s回滚已经同源。

PR 描述中的一个小节或存储库中的PERF.md都可以。重要的是下一个人(或下一个代理)在提议一个实验之前能读到它,而不会重复一个已经失败的实验。

步骤 5:防止回归

保护用户实际感受到的指标,而不是所有可用的数字。使用与修复所依据的相同的 LCP、INP、p95 延迟或其他主要指标。

当界面面向用户时,使用两个互补的层:

  • 合成 CI 门控: 在合并前通过性能预算捕获可重现的回归。重复嘈杂的测量或比较中间值/趋势,以便正常的运行间方差不会将门控变成一个不稳定的检查。
  • 现场监控: 对 RUM 数据中有意义的 p75 变化发出警报。使用归因的web-vitals数据定位原因;将 CrUX 的滚动窗口视为确认,而不是立即警报。

当任一防护措施触发时,返回步骤 1 并在提出另一个修复方案之前建立新的基线。

设置预算并强制执行:

JavaScript 包:< 200KB gzipped(初始加载)
CSS:< 50KB gzipped
图像:< 200KB 每张(折叠线以上)
字体:< 100KB 总计
API 响应时间:< 200ms (p95)
可交互时间:< 3.5s(在 4G 上)
Lighthouse 性能分数:≥ 90

在 CI 中强制执行:

# 包大小检查
npx bundlesize --config bundlesize.config.json

# Lighthouse CI
npx lhci autorun

另请参阅

有关详细的性能检查清单、优化命令和反模式参考,请参阅../../references/performance-checklist.md。

常见合理化

合理化现实
“我们以后会优化的”性能债务会不断累积。现在就修复明显的反模式,推迟微优化。
“在我的机器上速度很快”您的机器不是用户的。在具有代表性的硬件和网络上进行分析。
“这种优化是显而易见的”如果您没有测量,您就不会知道。先进行分析。
“用户不会注意到 100 毫秒”研究表明,100 毫秒的延迟会影响转化率。用户注意到的比您想象的多。
“框架处理性能”框架可以防止一些问题,但无法修复 N+1 查询或过大的包。
“查询慢,添加一个索引”先阅读计划。索引可能已经存在但无法使用,并且每个索引都会永久增加写入开销。
“只需缓存它”缓存一个已经很便宜的调用没有任何好处,反而会引入一个过期错误。缓存那些生成成本高并且读取频率远高于写入频率的内容。
“提高池大小,我们的连接不够用了”一个比数据库可以服务得更大的池会将队列移动到一个不那么可见的地方。找出什么在持有连接。
“它没有太大帮助,但也没有坏处”中性的更改应该回滚。您永远维护它们,却没有任何回报。
“我们已经写好了,不如留着”沉没成本。测量没有……

[截断以进行翻译]

Individual skills in this repo

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

alfathafp/portofolio-web-design

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

alfathafp/portofolio-web-design

Tests in real browsers via Chrome DevTools MCP. Use when building or debugging anything that runs in a browser. Use when you need to inspect the DOM, capture console errors, analyze network requests, profile performance, or verify visual output with real runtime data. Requires the chrome-devtools MCP server to be configured.

alfathafp/portofolio-web-design

Automates CI/CD pipeline setup. Use when setting up or modifying build and deployment pipelines. Use when you need to automate quality gates, configure test runners in CI, or establish deployment strategies.

alfathafp/portofolio-web-design

Conducts multi-axis code review. Use before merging any change. Use when reviewing code written by yourself, another agent, or a human. Use when you need to assess code quality across multiple dimensions before it enters the main branch. Use when asked to review a diff or a pull request, even when the diff is pasted inline.

alfathafp/portofolio-web-design

Simplifies code for clarity. Use when refactoring code for clarity without changing behavior. Use when code works but is harder to read, maintain, or extend than it should be. Use when reviewing code that has accumulated unnecessary complexity.

alfathafp/portofolio-web-design

Establishes a project's quality bar as a written contract and stops agents quietly lowering it. Interviews the user on which dimensions matter, supplies sane default thresholds when they have no number in mind, records everything in CONSTRAINTS.md, and watches the diff for a weakened bar — new @ts-ignore or eslint-disable suppressions, skipped or deleted tests, assertions stripped out, unimplemented stubs, thresholds edited down. Use when no quality bar is written down, when the user says "set up constraints" or "define our standards", when the user wants dimensions they care about — accessibility, web performance, coverage — set up as enforced constraints, when an agent keeps silencing checks or skipping tests to get to green, when you need a coverage or performance threshold and don't know what number to pick, or when an agent writes more code than anyone will read.

alfathafp/portofolio-web-design

Optimizes agent context setup. Use when starting a new session, when agent output quality degrades, when switching between tasks, or when you need to configure rules files and context for a project.

alfathafp/portofolio-web-design

Guides systematic root-cause debugging. Use when tests fail, builds break, something that worked yesterday broke, behavior doesn't match expectations, or you encounter any unexpected error. Use when you need to figure out what broke and why — a systematic approach to finding and fixing the root cause rather than guessing.

alfathafp/portofolio-web-design

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

alfathafp/portofolio-web-design

Manages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.

alfathafp/portofolio-web-design

Use when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.

alfathafp/portofolio-web-design

Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies

alfathafp/portofolio-web-design

Records decisions and documentation. Use when you need to document an architecture decision (ADR) or the reasoning behind a design choice, when changing public APIs, shipping features, or when you need to record context that future engineers and agents will need to understand the codebase.

alfathafp/portofolio-web-design

Subjects every non-trivial decision to a fresh-context adversarial review before it stands. Use when you want every assumption cross-examined before proceeding, when stress-testing a plan for hidden failure modes, when correctness matters more than speed, when working in unfamiliar code, when stakes are high (production auth, security-sensitive logic, a high-stakes migration, irreversible operations), or any time a confident output would be cheaper to verify now than to debug later.

alfathafp/portofolio-web-design

Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available

alfathafp/portofolio-web-design

Use when implementation is complete, all tests pass, and you need to decide how to integrate the work

alfathafp/portofolio-web-design

Builds production-quality, accessible, responsive user-facing UIs. Use when building or modifying interfaces and pages, creating components, implementing layouts, meeting WCAG accessibility requirements, managing state, or when the output needs to look and feel production-quality rather than AI-generated.

alfathafp/portofolio-web-design

Structures git workflow practices. Use when making any code change. Use when committing, branching, resolving conflicts, splitting uncommitted work in a messy working tree into clean atomic commits, opening or reviewing a pull request (PR), pushing to a remote, or when you need to organize work across multiple parallel streams. Use when cutting a release, choosing a semantic version bump, tagging, or writing a changelog.

alfathafp/portofolio-web-design

Refines raw ideas into sharp, actionable concepts through structured divergent and convergent thinking. Use when an idea is still vague, when you need to stress-test assumptions before committing to a plan, or when you want to expand options before converging on one. Triggers on "ideate", "refine this idea", or "stress-test my plan".

alfathafp/portofolio-web-design

Guides stable API and interface design. Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.

相关技能