/optimize
/optimize
Section titled “/optimize”Purpose
Section titled “Purpose”Analyze and optimize code performance. Identifies bottlenecks, suggests improvements, and implements optimizations while ensuring correctness.
/optimize [file-or-function]Arguments
Section titled “Arguments”- file-or-function: Path to file or function to optimize
- File path:
src/services/data-processor.ts - Function name:
processUserData - Module:
src/utils/
- File path:
Workflow
Section titled “Workflow”Step 1: Analyze Current Performance
Section titled “Step 1: Analyze Current Performance”-
Identify bottlenecks
- Analyze time complexity
- Check space complexity
- Look for redundant operations
-
Profile if possible
- Check for N+1 queries
- Identify unnecessary loops
- Find repeated calculations
Step 2: Identify Opportunities
Section titled “Step 2: Identify Opportunities”-
Algorithm improvements
- Better data structures
- More efficient algorithms
- Reduce complexity
-
Caching opportunities
- Memoization
- Result caching
- Computed properties
-
Async optimizations
- Parallel execution
- Batch operations
- Lazy loading
Step 3: Implement Optimizations
Section titled “Step 3: Implement Optimizations”-
Make targeted changes
- Implement improvements
- Preserve functionality
- Add comments
-
Verify improvements
- Test correctness
- Measure performance
- Compare before/after
Output
Section titled “Output”## Optimization Report: [Component]
### Current Performance- Time complexity: O(n²)- Space complexity: O(n)- Estimated execution time: 500ms for 1000 items- Issues found: 3
### Bottlenecks Identified
1. **Nested loop in data processing** - Location: `processData()` lines 45-60 - Impact: High - Current: O(n²)
2. **Redundant database queries** - Location: `getUserData()` lines 120-135 - Impact: Medium - Issue: N+1 query pattern
3. **Inefficient array operations** - Location: `filterResults()` lines 88-92 - Impact: Low - Issue: Multiple array iterations
### Optimizations Applied
#### 1. Algorithm Improvement**Before:**```typescript// O(n²) nested loopfor (const item of items) { for (const tag of tags) { if (item.tags.includes(tag)) { results.push(item); } }}After:
// O(n) with Set lookupconst tagSet = new Set(tags);const results = items.filter(item => item.tags.some(tag => tagSet.has(tag)));Impact: Time complexity reduced from O(n²) to O(n)
2. Caching Implementation
Section titled “2. Caching Implementation”Before:
function calculateScore(user) { // Recalculates every time return expensiveCalculation(user);}After:
const scoreCache = new Map();function calculateScore(user) { if (scoreCache.has(user.id)) { return scoreCache.get(user.id); } const score = expensiveCalculation(user); scoreCache.set(user.id, score); return score;}Impact: 95% cache hit rate in typical usage
3. Batch Processing
Section titled “3. Batch Processing”Before:
for (const userId of userIds) { await db.getUser(userId); // N queries}After:
const users = await db.getUsers(userIds); // 1 queryImpact: Reduced database queries from N to 1
Performance Improvements
Section titled “Performance Improvements”| Metric | Before | After | Improvement |
|---|---|---|---|
| Time Complexity | O(n²) | O(n log n) | 90% faster |
| Execution Time | 500ms | 50ms | 10x faster |
| Database Queries | 100 | 1 | 99% reduction |
| Memory Usage | 150MB | 80MB | 47% reduction |
Testing
Section titled “Testing”All existing tests pass:
- ✅ Unit tests: 45/45
- ✅ Integration tests: 12/12
- ✅ Performance benchmarks added
Recommendations
Section titled “Recommendations”- Monitor in production: Track actual performance metrics
- Consider further optimization: Implement pagination for large datasets
- Add benchmarks: Create performance regression tests
Files Modified
Section titled “Files Modified”src/services/data-processor.tstests/data-processor.test.ts(added benchmarks)
## Optimization Strategies
### 1. Algorithm Optimization- Replace inefficient algorithms- Use appropriate data structures- Reduce time/space complexity
### 2. Caching- Memoize expensive calculations- Cache API responses- Use computed properties
### 3. Database Optimization- Batch queries- Add indexes- Optimize query patterns- Use eager loading
### 4. Async Optimization- Parallel execution with `Promise.all()`- Lazy loading- Stream processing
### 5. Code-Level Optimization- Avoid premature optimization- Profile before optimizing- Focus on hot paths
## Examples
```bash# Optimize specific file/optimize src/services/report-generator.ts
# Optimize by function name/optimize calculateTotalSales
# Optimize entire module/optimize src/utils/
# Optimize with performance persona/optimize --persona=performance src/api/users.ts| Flag | Description |
|---|---|
--profile | Include profiling data |
--benchmark | Add performance benchmarks |
--aggressive | More aggressive optimizations |
--safe | Conservative optimizations only |
--persona=performance | Performance-focused analysis |
Best Practices
Section titled “Best Practices”- Profile First: Don’t optimize without measuring
- Preserve Correctness: Always verify behavior unchanged
- Add Tests: Include performance regression tests
- Document Changes: Explain why optimizations were made
- Measure Impact: Compare before/after metrics
Common Optimizations
Section titled “Common Optimizations”N+1 Query Prevention
Section titled “N+1 Query Prevention”// Before: N+1 queriesconst users = await db.getUsers();for (const user of users) { user.posts = await db.getPostsByUser(user.id);}
// After: 2 queriesconst users = await db.getUsers();const posts = await db.getPostsByUsers(users.map(u => u.id));// Map posts to usersMemoization
Section titled “Memoization”const memoize = (fn) => { const cache = new Map(); return (...args) => { const key = JSON.stringify(args); if (cache.has(key)) return cache.get(key); const result = fn(...args); cache.set(key, result); return result; };};Debouncing
Section titled “Debouncing”const debounce = (fn, delay) => { let timeout; return (...args) => { clearTimeout(timeout); timeout = setTimeout(() => fn(...args), delay); };};