Performance Optimization

Identify and fix performance bottlenecks in web APIs, database queries, and application code with optimization strategies and benchmarking plans in 2 minutes.

performance optimization profiling

Overview

Get detailed performance optimization plans for slow APIs, database queries, or application components. This template analyzes bottlenecks and provides specific code changes, trade-off analysis, and benchmarking strategies - all in under 2 minutes.

Use Cases

  • Fix slow REST API endpoints responding in 500ms+ when users expect sub-200ms response times
  • Optimize database queries with N+1 problems or missing indexes during sprint retrospectives
  • Reduce mobile app memory usage from 200MB to under 100MB before app store submission
  • Diagnose React component re-render performance issues causing UI lag
  • Speed up serverless functions hitting Lambda timeout limits
  • Optimize SQL queries for analytics dashboards loading slowly with large datasets

Benefits

Time Savings

  • Generate comprehensive optimization plans in 2 minutes instead of hours of manual analysis
  • Get 5-10 specific optimization strategies without reading documentation
  • Skip trial-and-error debugging with targeted bottleneck identification

Quality Improvements

  • Receive benchmarking plans with specific before/after metrics to track
  • Get code examples showing exact changes needed (eager loading, indexing, caching)
  • Understand trade-offs between speed, memory, and maintainability before implementing

Team Consistency

  • Maintain standard optimization approach across 5+ developers
  • Document performance decisions with clear metrics and rationale
  • Share optimization patterns that work across similar components

Template

Optimize performance for:

Component: {{component}}
Current performance: {{currentPerf}}

Performance issues:
{{issues}}

Target metrics:
{{targets}}

Include:
- Bottleneck analysis
- Optimization strategies
- Code changes needed
- Trade-offs
- Benchmarking plan
- Monitoring recommendations

Optimization focus: {{focus}}

Properties

  • component: Single-line Text
  • currentPerf: Single-line Text (default: Response time: 500ms)
  • issues: Multi-line Text
  • targets: Multi-line Text (default: Response time: <200ms Memory usage: <100MB)
  • focus: Multiple Selection (default: Speed, Memory)
    • Options: Speed, Memory, Network, Database, CPU, Disk I/O

Example Output

Here’s what you get when optimizing a slow API endpoint:

Input:

  • Component: User dashboard API endpoint
  • Current performance: Response time: 500ms
  • Issues: N+1 database queries, missing indexes, unoptimized JSON serialization
  • Target: Response time <200ms

Generated Output:

## Bottleneck Analysis

Primary Issues:
1. N+1 Queries - Each user metric triggers separate DB query (~300ms impact)
2. Missing Indexes - Sequential scans on user_id and created_at (~100ms impact)
3. Unoptimized Serialization - Large object conversion without field selection (~50ms)
4. Bloated Payload - Unnecessary nested data increases transfer time (~50ms)

## Optimization Strategy

### 1. Eliminate N+1 Queries
Before:
user = User.query.get(user_id)
metrics = [metric.to_dict() for metric in user.metrics]

After:
user = User.query.options(
    joinedload(User.metrics)
).filter_by(id=user_id).first()

### 2. Add Database Indexes
CREATE INDEX idx_metrics_user_created ON metrics(user_id, created_at DESC);

### 3. Optimize Serialization
return {
    'id': user.id,
    'metrics': [{'id': m.id, 'value': m.value} for m in user.metrics[:10]]
}

## Trade-offs

| Optimization | Benefit | Trade-off |
|-------------|---------|-----------|
| Eager loading | -60% query time | +10% memory per request |
| DB indexes | -20% query time | +5% write time |
| Pagination | -50% payload size | Multiple requests for full data |

## Benchmarking Plan
ab -n 1000 -c 10 http://localhost:8000/api/dashboard
Target: <200ms avg response time

Common Mistakes

Optimizing Without Measurement Many developers guess at bottlenecks rather than profiling first. Start with actual metrics from production logs or APM tools. You might assume your database is slow when the real issue is inefficient JSON serialization taking 300ms per request.

Premature Optimization You don’t need to optimize code that runs once per hour and takes 2 seconds. Focus on hot paths - API endpoints called 1000+ times per day or functions in tight loops. The 80/20 rule applies: 20% of your code causes 80% of performance issues.

Ignoring Trade-offs Every optimization has costs. Adding database indexes speeds reads but slows writes. Caching reduces latency but increases memory and stale data risk. Eager loading reduces queries but increases memory per request. Document these trade-offs so future developers understand the decisions.

Missing Benchmarks Optimization without before/after metrics is just guessing. Use tools like Apache Bench, wrk, or language-specific profilers to get real numbers. Run benchmarks multiple times to account for variance.

Over-optimizing Single Metrics Reducing response time from 50ms to 25ms rarely matters to users, but doubling code complexity always hurts maintainability. Target user-perceptible improvements (200ms β†’ 100ms) and stop when you hit diminishing returns.

Frequently Used With

  • Code Review - Review optimization code changes for correctness and maintainability
  • Refactoring Plan - Plan larger refactoring needed to implement performance fixes
  • Unit Test - Write tests to prevent performance regressions after optimization

Get Early Access to Migi

Want to use this template and hundreds more directly in your workflow? Get early access to Migi - the prompt management tool that integrates with your IDE, terminal, and browser.

Explore more Coding templates or browse all templates.