Performance Profiling and Optimization Guide
Get a systematic approach to profiling and optimizing application performance — from identifying bottlenecks to implementing fixes with measurable before/after benchmarks.
Prompt Template
You are a senior performance engineer. Help me systematically profile and optimize the performance of my application. Application context: - Type: [web app / mobile app / API / CLI tool / microservice] - Tech stack: [e.g., Next.js + Node.js + PostgreSQL / Rails + Redis / Python FastAPI] - Current performance issue: [slow page loads / high API latency / memory leaks / slow database queries / high CPU usage] - Scale: [requests per second / concurrent users / data volume] - Where it's deployed: [Vercel / AWS / self-hosted / Docker / Kubernetes] - Current metrics (if available): [e.g., P95 latency: 2.3s, TTFB: 800ms, memory usage: 1.2GB] - Performance budget/target: [e.g., P95 < 500ms, LCP < 2.5s, memory < 512MB] Please provide: 1. **Profiling Strategy** — A step-by-step plan to identify exactly where time/resources are being spent: - Recommended profiling tools for my stack - What to measure first (the "big rocks") - How to create a reproducible benchmark baseline - Flame graph / trace analysis walkthrough 2. **Common Bottleneck Checklist** — The most likely performance issues for my stack, ordered by probability: - For each: How to detect it, what the symptoms look like, and the fix - Include database, network, compute, and memory categories 3. **Optimization Plan** — Prioritized list of optimizations ranked by impact/effort ratio: - Quick wins (< 1 hour, high impact) - Medium effort (1 day, significant impact) - Architecture changes (1 week+, transformative impact) - For each: Expected improvement, implementation steps, and how to verify the fix 4. **Monitoring & Regression Prevention** — How to ensure performance stays good: - Key metrics to alert on - Performance CI checks (e.g., Lighthouse CI, k6 in CI pipeline) - Budget enforcement automation 5. **Before/After Template** — A documentation template for recording each optimization with baseline metrics, changes made, and measured results.
Example Output
# Performance Optimization Plan: Next.js + Node.js + PostgreSQL
Current: P95 = 2.3s | Target: P95 < 500ms
1. Profiling Strategy
Tools to Set Up
| Layer | Tool | What It Tells You |
|-------|------|-------------------|
| Frontend | Chrome DevTools Performance tab | Component render times, layout thrashing, JS execution |
| Frontend | Web Vitals (CrUX / `web-vitals` lib) | Real-user LCP, FID, CLS |
| API | Node.js `--inspect` + Chrome DevTools | CPU flame graphs, async bottlenecks |
| Database | `EXPLAIN ANALYZE` + `pg_stat_statements` | Slow queries, missing indexes, seq scans |
| Full stack | OpenTelemetry + Jaeger | Distributed traces showing where time is spent across services |
Baseline Benchmark
# Install k6 for load testing
brew install k6
# Create baseline test
cat > baseline.js << 'EOF'
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 50 },
{ duration: '1m', target: 50 },
{ duration: '30s', target: 0 },
],
};
export default function () {
const res = http.get('https://your-app.com/api/main-endpoint');
check(res, { 'status 200': (r) => r.status === 200 });
sleep(1);
}
EOF
k6 run baseline.js
Save output as `perf-baseline-2026-03-28.txt`
2. Bottleneck Checklist (Next.js + Node + PostgreSQL)
🔴 Most Likely Culprits
1. N+1 Database Queries (probability: 85%)
- Symptom: API response time scales linearly with data size
- Detect: Enable query logging, count queries per request
- Fix: Use `JOIN` or batch queries; add DataLoader pattern
2. Missing Database Indexes (probability: 80%)
- Symptom: Queries > 100ms on tables with > 10K rows
- Detect: `EXPLAIN ANALYZE` shows Seq Scan on large tables
- Fix: Add indexes on WHERE/JOIN/ORDER BY columns
-- Find slow queries
SELECT query, mean_exec_time, calls
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 20;
3. No API Response Caching (probability: 75%)
- Symptom: Same expensive computation on every request
- Fix: Add Redis cache with TTL for stable data
3. Optimization Plan
Quick Wins (< 1 hour each)
| # | Optimization | Expected Impact | Effort |
|---|-------------|-----------------|--------|
| 1 | Add missing indexes for top 5 slow queries | -40% DB time | 30 min |
| 2 | Enable Next.js `output: 'standalone'` + compression | -30% TTFB | 15 min |
| 3 | Add `Cache-Control` headers for static API responses | -50% for cached routes | 20 min |
| 4 | Set `connection: keep-alive` on DB pool (pool size = 20) | -15% connection overhead | 10 min |
Medium Effort (1 day)
| # | Optimization | Expected Impact | Effort |
|---|-------------|-----------------|--------|
| 5 | Implement Redis caching for top 3 expensive endpoints | -60% P95 latency | 4 hours |
| 6 | Fix N+1 queries with JOINs or DataLoader | -50% DB query count | 4 hours |
| 7 | Add ISR (Incremental Static Regeneration) for content pages | -80% TTFB for those pages | 3 hours |
Tips for Best Results
- 💡Always measure before optimizing — gut feelings about performance bottlenecks are wrong more often than right.
- 💡Fix database queries first: in 80% of web apps, the database is the bottleneck, not the application code.
- 💡Use P95/P99 latency, not averages — averages hide the pain your worst-experience users feel.
- 💡Add a performance budget to your CI pipeline early — it's 10x easier to prevent regressions than to fix them after months of accumulated slowdowns.
Related Prompts
Code Review Assistant
Get a thorough, senior-level code review with actionable feedback on quality, security, performance, and best practices.
Debugging Detective
Systematically debug errors and unexpected behavior with root cause analysis and fix suggestions.
Code Refactoring Advisor
Transform messy, complex code into clean, maintainable, well-structured code with clear explanations.