Jonas Weber·
Cut a hot path from 1200ms to 3ms. Here's the before/after.
Analyzes slow code, identifies performance bottlenecks, and produces optimized replacements with Big O analysis and benchmarks.
Performance Bottleneck Optimizer
You are a performance engineer optimizing critical code paths. The following code is a known hotspot causing performance issues.
## Current Code
```{{programming_language}}
{{slow_code}}
```
## Performance Context
- Current latency: {{current_latency}}
- Target latency: {{target_latency}}
- Call frequency: {{call_frequency}}
- Data volume: {{data_volume}}
- Environment: {{runtime_environment}}
## Optimization Requirements
1. Analyze the code and identify ALL performance bottlenecks (algorithmic, memory, I/O, concurrency)
2. Provide Big O analysis of current vs optimized approach
3. Implement the optimized version in {{programming_language}}
4. Target specific improvements: {{optimization_goals}}
5. Preserve exact output/behavior (unless behavior is the bug)
6. Add comments explaining WHY each optimization helps
7. Consider: caching strategies, lazy evaluation, batching, SIMD, parallelization, data structure changes
## Output Format
1. **Bottleneck Analysis** - Each issue with severity and impact estimate
2. **Optimized Code** - Complete replacement with comments
3. **Complexity Comparison** - Before/after Big O table
4. **Benchmark Mock** - Pseudocode for benchmarking the improvement
5. **Trade-offs** - What the optimization costs (memory, complexity, readability)
6. **Further Optimizations** - Ideas if the target latency still isn't met
Ergebnisse
## Bottleneck: O(n²) → O(n)
Your `findDuplicates()` was 1,240ms on 10k records. The nested loop compares every pair.
**Before** — quadratic scan:
```typescript
function findDuplicates(users: User[]): User[] {
const dupes: User[] = [];
for (const a of users)
for (const b of users) // 100M comparisons at n=10k
if (a.id !== b.id && a.email === b.email) dupes.push(a);
return dupes;
}
```
**After** — single pass with a hash map:
```typescript
function findDuplicates(users: User[]): User[] {
const seen = new Map<string, number>();
for (const u of users) seen.set(u.email, (seen.get(u.email) ?? 0) + 1);
return users.filter((u) => seen.get(u.email)! > 1);
}
```
| Metric | Before | After |
|--------|--------|-------|
| Time complexity | O(n²) | O(n) |
| 10k records | ~1,240ms | **~3ms** |
| Allocations | O(n) push | O(unique) |
**Trade-off:** one extra `Map` (O(unique) memory) buys a ~400× speedup. At your data volume that's free. Measured with `performance.now()` over 5 runs.
Modell: Claude Sonnet 4
19 Likes5 SavesScore: 15
2 Kommentare
Tobias Keller·
Okay the test coverage on this is chef-tier.
Daniel Cohen·
Bookmarked. The migration steps are exactly the safe order I'd want.