| name | performance-optimization |
| description | Profiles, reduces frontend/backend costs: split bundles, optimize assets, apply caching, fix Core Web Vitals regressions. Use when profiling Lighthouse/CI regressions, reducing bundle size, or fixing high CLS/LCP/TTI metrics. |
Performance Optimization
Rule: Measure first (Chrome DevTools, Lighthouse, Datadog), optimize second. Set budgets (load time, memory, API latency). Automate in CI/CD.
Domain-specific patterns (rendering, JS, Node optimizations) referenced in REFERENCE.md to keep this skill concise.
Patterns by Domain
| Domain | Key patterns |
|---|
| Rendering | React.memo/useMemo/useCallback only after profiling; stable key props; CSS classes over inline styles; CSS animations (GPU); requestIdleCallback for non-critical work |
| Assets | WebP/AVIF images; SVG icons; bundle+minify+tree-shake (esbuild/Rollup); loading="lazy"; dynamic imports; long-lived cache headers + cache-busting; font subsetting + font-display: swap |
| JS | Web Workers for heavy computation; debounce/throttle events; clean up listeners/intervals; Map/Set for lookups; TypedArray for numeric data |
| Node.js | Async APIs only (never readFileSync in prod); clustering/worker threads for CPU; streams for large I/O; profile with clinic.js / node --inspect |
Debounce Example
input.addEventListener('input', (e) => fetch(`/search?q=${e.target.value}`));
let t; input.addEventListener('input', (e) => { clearTimeout(t); t = setTimeout(() => fetch(`/search?q=${e.target.value}`), 300); });
Executable Examples
Dynamic import splitting (example)
import dynamic from 'next/dynamic';
const Chart = dynamic(() => import('../components/Chart'), { ssr: false, loading: () => <div>Loading chart…</div> });
export default function Page(){ return <Chart />; }
React.memo + profiler pattern
import React, { Profiler } from 'react';
const Item = React.memo(function Item({data}){ return <div>{data.title}</div>; });
function onRender(id, phase, actualDuration){ console.log(id, phase, actualDuration); }
export default function List({items}){
return (
<Profiler id="List" onRender={onRender}>
{items.map(i=> <Item key={i.id} data={i} />)}
</Profiler>
);
}
Profiling Workflow (step-by-step)
- Run Lighthouse (or CI perf job); record baseline.
- Checkpoint: failing metric(s) identified (LCP/CLS/FID/TTI).
- Recovery: if noisy, reproduce locally with
--emulated-form-factor=mobile.
- Profile with DevTools Profiler / React profiler or Node
clinic for backend.
- Checkpoint: hotspot call stacks / long tasks located.
- Apply minimal fix (code-split, memoize, reduce payloads, defer non-critical work).
- Checkpoint: targeted change reduces measured hotspot time in profiler.
- Re-run Lighthouse/CI perf job; compare; set threshold (e.g., 10% improvement or within budget).
- If regression persists, iterate; create rollback plan; note fixes in changelog.
Review Checklist
References