Skip to content

/optimize

Analyze and optimize code performance. Identifies bottlenecks, suggests improvements, and implements optimizations while ensuring correctness.

Terminal window
/optimize [file-or-function]
  • file-or-function: Path to file or function to optimize
    • File path: src/services/data-processor.ts
    • Function name: processUserData
    • Module: src/utils/
  1. Identify bottlenecks

    • Analyze time complexity
    • Check space complexity
    • Look for redundant operations
  2. Profile if possible

    • Check for N+1 queries
    • Identify unnecessary loops
    • Find repeated calculations
  1. Algorithm improvements

    • Better data structures
    • More efficient algorithms
    • Reduce complexity
  2. Caching opportunities

    • Memoization
    • Result caching
    • Computed properties
  3. Async optimizations

    • Parallel execution
    • Batch operations
    • Lazy loading
  1. Make targeted changes

    • Implement improvements
    • Preserve functionality
    • Add comments
  2. Verify improvements

    • Test correctness
    • Measure performance
    • Compare before/after
## 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 loop
for (const item of items) {
for (const tag of tags) {
if (item.tags.includes(tag)) {
results.push(item);
}
}
}

After:

// O(n) with Set lookup
const 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)

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

Before:

for (const userId of userIds) {
await db.getUser(userId); // N queries
}

After:

const users = await db.getUsers(userIds); // 1 query

Impact: Reduced database queries from N to 1

MetricBeforeAfterImprovement
Time ComplexityO(n²)O(n log n)90% faster
Execution Time500ms50ms10x faster
Database Queries100199% reduction
Memory Usage150MB80MB47% reduction

All existing tests pass:

  • ✅ Unit tests: 45/45
  • ✅ Integration tests: 12/12
  • ✅ Performance benchmarks added
  1. Monitor in production: Track actual performance metrics
  2. Consider further optimization: Implement pagination for large datasets
  3. Add benchmarks: Create performance regression tests
  • src/services/data-processor.ts
  • tests/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
FlagDescription
--profileInclude profiling data
--benchmarkAdd performance benchmarks
--aggressiveMore aggressive optimizations
--safeConservative optimizations only
--persona=performancePerformance-focused analysis
  1. Profile First: Don’t optimize without measuring
  2. Preserve Correctness: Always verify behavior unchanged
  3. Add Tests: Include performance regression tests
  4. Document Changes: Explain why optimizations were made
  5. Measure Impact: Compare before/after metrics
// Before: N+1 queries
const users = await db.getUsers();
for (const user of users) {
user.posts = await db.getPostsByUser(user.id);
}
// After: 2 queries
const users = await db.getUsers();
const posts = await db.getPostsByUsers(users.map(u => u.id));
// Map posts to users
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;
};
};
const debounce = (fn, delay) => {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
};
  • /review - Code review with performance checks
  • /test - Add performance tests
  • /fix - Fix performance issues