Performance Optimization Skill
Comprehensive frameworks for analyzing and optimizing application performance across the entire stack.
When to Use
- Application feels slow or unresponsive
- Database queries taking too long
- Frontend bundle size too large
- API response times exceed targets
- Core Web Vitals need improvement
- Preparing for scale or high traffic
Performance Targets
Core Web Vitals (Frontend)
| Metric |
Good |
Needs Work |
| LCP (Largest Contentful Paint) |
< 2.5s |
< 4s |
| INP (Interaction to Next Paint) |
< 200ms |
< 500ms |
| CLS (Cumulative Layout Shift) |
< 0.1 |
< 0.25 |
| TTFB (Time to First Byte) |
< 200ms |
< 600ms |
Backend Targets
| Operation |
Target |
| Simple reads |
< 100ms |
| Complex queries |
< 500ms |
| Write operations |
< 200ms |
| Index lookups |
< 10ms |
Bottleneck Categories
| Category |
Symptoms |
Tools |
| Network |
High TTFB, slow loading |
Network tab, WebPageTest |
| Database |
Slow queries, pool exhaustion |
EXPLAIN ANALYZE, pg_stat_statements |
| CPU |
High usage, slow compute |
Profiler, flame graphs |
| Memory |
Leaks, GC pauses |
Heap snapshots |
| Rendering |
Layout thrashing |
React DevTools, Performance tab |
Database Optimization
Key Patterns
- Add Missing Indexes - Turn
Seq Scan into Index Scan
- Fix N+1 Queries - Use JOINs or
include instead of loops
- Cursor Pagination - Never load all records
- Connection Pooling - Manage connection lifecycle
Quick Diagnostics
-- Find slow queries (PostgreSQL)
SELECT query, calls, mean_time / 1000 as mean_seconds
FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10;
-- Verify index usage
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 123;
See templates/database-optimization.ts for N+1 fixes and pagination patterns
Caching Strategy
Cache Hierarchy
L1: In-Memory (LRU, memoization) - fastest
L2: Distributed (Redis/Memcached) - shared
L3: CDN (edge, static assets) - global
L4: Database (materialized views) - fallback
Cache-Aside Pattern
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const data = await db.query(...);
await redis.setex(key, 3600, JSON.stringify(data));
return data;
See templates/caching-patterns.ts for full implementation
Frontend Optimization
Bundle Optimization
- Code Splitting -
lazy() for route-based splitting
- Tree Shaking - Import only what you need
- Image Optimization - WebP/AVIF, lazy loading, proper sizing
Rendering Optimization
- Memoization -
memo(), useCallback(), useMemo()
- Virtualization - Render only visible items in long lists
- Batch DOM Operations - Read all, then write all
See templates/frontend-optimization.tsx for patterns
Analysis Commands
# Lighthouse audit
lighthouse http://localhost:3000 --output=json
# Bundle analysis
npx @next/bundle-analyzer # Next.js
npx vite-bundle-visualizer # Vite
API Optimization
Response Optimization
- Field Selection - Return only requested fields
- Compression - Enable gzip/brotli (threshold: 1KB)
- ETags - Enable 304 responses for unchanged data
- Pagination - Cursor-based for large datasets
See templates/api-optimization.ts for middleware examples
Monitoring Checklist
Before Launch
Ongoing
See templates/performance-metrics.ts for Prometheus metrics setup
Extended Thinking Triggers
Use Opus 4.5 extended thinking for:
- Complex debugging - Multiple potential causes
- Architecture decisions - Caching strategy selection
- Trade-off analysis - Memory vs CPU vs latency
- Root cause analysis - Performance regression investigation
Templates Reference
| Template |
Purpose |
database-optimization.ts |
N+1 fixes, pagination, pooling |
caching-patterns.ts |
Redis cache-aside, memoization |
frontend-optimization.tsx |
React memo, virtualization, code splitting |
api-optimization.ts |
Compression, ETags, field selection |
performance-metrics.ts |
Prometheus metrics, performance budget |
1---2name: performance-optimization3description: Full-stack performance analysis, optimization patterns, and monitoring strategies4version: 1.0.05category: Quality & Optimization6agents: [backend-system-architect, frontend-ui-developer, code-quality-reviewer]7keywords: [performance, optimization, speed, latency, throughput, caching, profiling, bundle, Core Web Vitals]8metadata:9 source: https://skillmd.com/skills/ariegoldkin/performance-optimization10---1112# Performance Optimization Skill1314Comprehensive frameworks for analyzing and optimizing application performance across the entire stack.1516## When to Use1718- Application feels slow or unresponsive19- Database queries taking too long20- Frontend bundle size too large21- API response times exceed targets22- Core Web Vitals need improvement23- Preparing for scale or high traffic2425## Performance Targets2627### Core Web Vitals (Frontend)2829| Metric | Good | Needs Work |30|--------|------|------------|31| **LCP** (Largest Contentful Paint) | < 2.5s | < 4s |32| **INP** (Interaction to Next Paint) | < 200ms | < 500ms |33| **CLS** (Cumulative Layout Shift) | < 0.1 | < 0.25 |34| **TTFB** (Time to First Byte) | < 200ms | < 600ms |3536### Backend Targets3738| Operation | Target |39|-----------|--------|40| Simple reads | < 100ms |41| Complex queries | < 500ms |42| Write operations | < 200ms |43| Index lookups | < 10ms |4445## Bottleneck Categories4647| Category | Symptoms | Tools |48|----------|----------|-------|49| **Network** | High TTFB, slow loading | Network tab, WebPageTest |50| **Database** | Slow queries, pool exhaustion | EXPLAIN ANALYZE, pg_stat_statements |51| **CPU** | High usage, slow compute | Profiler, flame graphs |52| **Memory** | Leaks, GC pauses | Heap snapshots |53| **Rendering** | Layout thrashing | React DevTools, Performance tab |5455## Database Optimization5657### Key Patterns58591. **Add Missing Indexes** - Turn `Seq Scan` into `Index Scan`602. **Fix N+1 Queries** - Use JOINs or `include` instead of loops613. **Cursor Pagination** - Never load all records624. **Connection Pooling** - Manage connection lifecycle6364### Quick Diagnostics6566```sql67-- Find slow queries (PostgreSQL)68SELECT query, calls, mean_time / 1000 as mean_seconds69FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10;7071-- Verify index usage72EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 123;73```7475> See `templates/database-optimization.ts` for N+1 fixes and pagination patterns7677## Caching Strategy7879### Cache Hierarchy8081```82L1: In-Memory (LRU, memoization) - fastest83L2: Distributed (Redis/Memcached) - shared84L3: CDN (edge, static assets) - global85L4: Database (materialized views) - fallback86```8788### Cache-Aside Pattern8990```typescript91const cached = await redis.get(key);92if (cached) return JSON.parse(cached);93const data = await db.query(...);94await redis.setex(key, 3600, JSON.stringify(data));95return data;96```9798> See `templates/caching-patterns.ts` for full implementation99100## Frontend Optimization101102### Bundle Optimization1031041. **Code Splitting** - `lazy()` for route-based splitting1052. **Tree Shaking** - Import only what you need1063. **Image Optimization** - WebP/AVIF, lazy loading, proper sizing107108### Rendering Optimization1091101. **Memoization** - `memo()`, `useCallback()`, `useMemo()`1112. **Virtualization** - Render only visible items in long lists1123. **Batch DOM Operations** - Read all, then write all113114> See `templates/frontend-optimization.tsx` for patterns115116### Analysis Commands117118```bash119# Lighthouse audit120lighthouse http://localhost:3000 --output=json121122# Bundle analysis123npx @next/bundle-analyzer # Next.js124npx vite-bundle-visualizer # Vite125```126127## API Optimization128129### Response Optimization1301311. **Field Selection** - Return only requested fields1322. **Compression** - Enable gzip/brotli (threshold: 1KB)1333. **ETags** - Enable 304 responses for unchanged data1344. **Pagination** - Cursor-based for large datasets135136> See `templates/api-optimization.ts` for middleware examples137138## Monitoring Checklist139140### Before Launch141142- [ ] Lighthouse score > 90143- [ ] Core Web Vitals pass144- [ ] Bundle size within budget145- [ ] Database queries profiled146- [ ] Compression enabled147- [ ] CDN configured148149### Ongoing150151- [ ] Performance monitoring active152- [ ] Alerting for degradation153- [ ] Lighthouse CI in pipeline154- [ ] Weekly query analysis155- [ ] Real User Monitoring (RUM)156157> See `templates/performance-metrics.ts` for Prometheus metrics setup158159## Extended Thinking Triggers160161Use Opus 4.5 extended thinking for:162- **Complex debugging** - Multiple potential causes163- **Architecture decisions** - Caching strategy selection164- **Trade-off analysis** - Memory vs CPU vs latency165- **Root cause analysis** - Performance regression investigation166167## Templates Reference168169| Template | Purpose |170|----------|---------|171| `database-optimization.ts` | N+1 fixes, pagination, pooling |172| `caching-patterns.ts` | Redis cache-aside, memoization |173| `frontend-optimization.tsx` | React memo, virtualization, code splitting |174| `api-optimization.ts` | Compression, ETags, field selection |175| `performance-metrics.ts` | Prometheus metrics, performance budget |