Community程式設計與開發github.com

alfathafp/portofolio-web-design

全面優化應用程式效能,涵蓋前端、後端、查詢與資料庫。適合在有效能需求、懷疑效能回歸、需改善 Core Web Vitals 或載入時間、需修復 N+1 查詢模式,或分析後發現瓶頸時使用。

portofolio-web-design 是什麼?

這是一個名為「效能優化」的 AI agent 技能,旨在幫助開發者與 AI 代理以系統化、數據驅動的方式進行效能調校。其核心原則為「先測量再優化」,透過五步驟工作流程:測量基準、識別瓶頸、修正問題、驗證結果與防止回歸,確保每一次變更都是基於實證。此技能支援多種平台,包括 Chrome DevTools、Lighthouse、web-vitals 函式庫與 APM 工具,並提供針對前端(如 bundle 大小、渲染效能、圖片優化)與後端(如 N+1 查詢、索引使用、連線池耗盡)常見瓶頸的詳細診斷指南與修復模式。團隊可將效能預算整合至 CI 流程中,實現自動化回歸偵測,並保留一份「績效紀錄」以避免重複嘗試無效的優化方案。

相容平台~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)
  • 使用者或監控系統回報反應遲緩
  • Core Web Vitals 評分低於門檻
  • 您懷疑某項變更引入了效能回歸
  • 正在建置處理大量資料或高流量的功能

何時不該使用: 在沒有證據顯示問題存在時,請勿進行優化。過早優化會增加複雜性,其成本遠超過所帶來的效能提升。

Core Web Vitals 目標

指標良好待改善不佳
LCP (最大內容繪製)≤ 2.5 秒≤ 4.0 秒> 4.0 秒
INP (下次繪製互動)≤ 200 毫秒≤ 500 毫秒> 500 毫秒
CLS (累計版面配置位移)≤ 0.1≤ 0.25> 0.25

優化工作流程

1. 測量  → 使用真實數據建立基準
2. 識別  → 找出實際瓶頸(非假定)
3. 修正  → 針對特定瓶頸處理
4. 驗證  → 再次測量;保留或復原
5. 防護  → 新增監控或測試以防止回歸

第一步:測量

兩種互補的方法——請同時使用:

  • 合成測試(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 凍結? --> 剖析主執行緒,尋找長時間任務 (>50 毫秒)
│   ├── 表單輸入延遲? --> 檢查重新渲染、受控元件的開銷
│   └── 動畫卡頓? --> 檢查佈局抖動、強制重排
├── 導航後頁面
│   ├── 資料載入? --> 測量 API 回應時間,檢查瀑布效應
│   └── 客戶端渲染? --> 剖析元件渲染時間,檢查 N+1 次擷取
└── 後端 / API
    ├── 單一端點緩慢? --> 剖析資料庫查詢,檢查索引
    ├── 所有端點都緩慢? --> 檢查連線池、記憶體、CPU
    └── 間歇性緩慢? --> 檢查鎖定競爭、垃圾回收暫停、外部依賴

第二步:識別瓶頸

按類別劃分的常見瓶頸:

前端:

症狀可能原因調查方向
LCP 緩慢大量圖片、渲染阻塞資源、伺服器緩慢檢查網路瀑布圖、圖片大小
CLS 偏高未指定尺寸的圖片、後載入內容、字型位移檢查佈局位移歸因
INP 不佳主執行緒上大量 JavaScript、大型 DOM 更新檢查效能追蹤中的長時間任務
初始載入緩慢大型套件、大量網路請求檢查套件大小、程式碼分割

後端:

症狀可能原因調查方向
API 回應緩慢N+1 查詢、缺少索引、未優化的查詢檢查資料庫查詢日誌
記憶體增長遺漏的參考、無限制的快取、大型負載堆積快照分析
CPU 飆升同步大量計算、正則表達式回溯CPU 效能剖析
高延遲缺少快取、冗餘計算、網路跳躍追蹤請求在堆疊中的路徑

第三步:修正瓶頸

只修正第二步所識別出的單一問題,不要做其他事。以下的反模式是常見的嫌疑對象;每個項目都提供了規則、識別特徵,並連結到位於 references/optimization-patterns.md 的實際修復範例。僅在處理相關程式碼時才開啟需要的連結,不要預先開啟。

後端

  • N+1 查詢。 每個資料列執行一次查詢是最常見的後端瓶頸。應在同一個查詢中擷取關聯資料(使用 join / include),而非在迴圈內執行。模式。
  • 無限制的資料擷取。 每個列表端點都應進行分頁,並附帶限制與穩定的排序順序。模式。
  • 忽略索引的查詢。 「加個索引」只是猜測;EXPLAIN ANALYZE 才是測量。看到預期應使用索引的 Seq Scan、rows= 估算值與實際相差一個數量級,以及掃描上方的 Sort 節點,各自需要不同的修正;糟糕的 rows= 估算值意味著統計資料過時,因此應執行 ANALYZE 而非添加索引。應根據查詢的形狀來建立索引(先放等值條件欄位,再放範圍或排序欄位)。對於選擇性極低的主要值(需使用部分索引)、開頭為萬用字元(需要三元組或全文檢索),或對欄位套用函式(如 WHERE lower(email) = ?,需對運算式建立索引),普通索引將無法發揮作用。每個索引都會增加寫入操作的負擔。建立索引後需重新檢視執行計畫;如果索引沒有改變結果,就應該復原它。模式。
  • 連線池耗盡。 徵兆:所有端點同時變慢,時間花在等待連線而非執行查詢,且資料庫顯示大量空閒連線。每個程序一個連線池,其大小應確保 實例數 × 最大值 不超過資料庫的連線上限。並非越大越好;這會將佇列移到資料庫端,使其更難觀察。若實例數量無上限(如無伺服器架構、自動擴展),應透過代理(pgbouncer、RDS Proxy)進行多工處理,而非提高 max。模式。
  • 缺少快取。 快取那些產生成本高昂,且讀取次數遠多於變更次數的內容;為一個已經很快的查詢加入快取只會增加網路跳躍與資料過時的問題,徒勞無功。應審慎選擇快取層級(程序內、共享、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 命名匯入進行 Tree Shaking;真正的效益來自於路由級別的程式碼分割,以及透過 Suspense 懶載入大型、不常用的功能。在改變匯入風格前,請先進行效能剖析。模式。

第四步:驗證(保留或復原)

在重新測量之前,任何修復都只是一個假設。這一步驟將決定它是否應該被保留。

使用與建立基準時相同的方式重新測量: 相同的指令、相同的條件、相同的固定預算(時鐘時間、樣本數或請求數)。在冷快取上建立的基準,與在熱快取上取得的結果相比較,測量到的是快取本身,而非您的變更。

一次只改變一件事。 同時部署三項優化只會得到一個數字,而您無法歸因於其中任何一項。如果它們必須一起發布,請先單獨測量每一項。

戰勝雜訊,而不僅僅是平均值。 重複測量,並將結果差異與執行間變異進行比較。在 ±5% 的變異範圍內得到 3% 的增益並不是真正的增益;那只不過是抽樣誤差。

然後,嚴格做出決定:

結果 vs. 基準行動
超過門檻,測試通過保留。 在提交訊息中附上改善前後的數值。
在雜訊範圍內(無顯著變化)復原。
變差復原。
改善,但某項測試失敗復原。 這是偽裝成勝利的回歸。

「沒有影響」等同於「復原」,而非「保留」。 這是團隊經常跳過的步驟:變更已經寫好,丟掉它感覺很浪費,於是它在未經測量的情況下被合併,導致程式碼庫累積了從未帶來任何好處的複雜性。您保留的程式碼,您得永久維護它。讓它能為自己付出代價。

正確性高於指標。 測試套件必須保持綠色並且指標有實際進步。一個透過省略產品所需工作來獲勝的「優化」(跳過驗證、快取必須保持新鮮的資料、移除具有負載均衡作用的 await)是回歸,而非勝利。

記錄每一次嘗試,包括那些被復原的

被復原的工作在 git 歷史中不會留下痕蹟,這正是為什麼同一個已被放棄的點子會在下個季度再次被嘗試。保留一份簡短的記錄,這樣被放棄的想法就會永遠被放棄:

想法基準 → 結果結論原因
對列表行元件進行 MemoizeINP 240ms → 235ms復原在雜訊範圍內 (±15ms)。行元件並非瓶頸。
對列表進行虛擬化INP 240ms → 90ms保留追蹤中長時間任務已消失。
預先連接到 API 來源LCP 2.8s → 2.8s復原已經是同源。

在 PR 描述中的一個章節或 repo 中的 PERF.md 檔案都可以。重要的是,下一個人(或下一個 AI 代理)在提議進行實驗之前,能夠讀到這份記錄,而不會重複執行已經失敗過的實驗。

第五步:防止回歸

守護用戶實際感受到的指標,而不是每一個可用的數字。使用與當初證明修復效益相同的 LCP、INP、p95 延遲或其他主要指標。

當涉及使用者介面時,使用兩個互補的層級:

  • 合成 CI 閘道: 在合併前透過效能預算捕捉可重現的回歸。重複執行結果不穩定的測量,或比較中位數/趨勢,以避免正常的執行間變異使閘道變成不穩定的檢查。
  • 現場監控: 對 RUM 數據中具有意義的 p75 變化發出警報。使用帶有歸因的 web-vitals 數據來定位原因;將 CrUX 的滾動窗口視為確認信號,而非即時警報。

當任一防護機制觸發時,回到第一步並在提出另一個修復方案前,先建立一個新的基準。

設定預算並強制執行:

JavaScript 套件:< 200KB(gzip 壓縮後,首次載入)
CSS:< 50KB(gzip 壓縮後)
圖片:< 200KB(每張,摺疊線以上)
字型:< 100KB(總計)
API 回應時間:< 200ms(p95)
可互動時間:< 3.5 秒(在 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.

相關技能