Code Optimization
Analyze code for performance issues following this priority order:
Analysis Priorities
- •Performance bottlenecks - O(n²) operations, inefficient loops, unnecessary iterations
- •Memory leaks - unreleased resources, circular references, growing collections
- •Algorithm improvements - better algorithms or data structures for the use case
- •Caching opportunities - repeated computations, redundant I/O, memoization candidates
- •Concurrency issues - race conditions, deadlocks, thread safety problems
Workflow
- •Read the target code file(s)
- •Identify language and framework context
- •Analyze for each priority category
- •Report findings with severity and fixes
Response Format
For each issue found:
code
### [Severity] Issue Title **Location**: file:line_number **Category**: Performance | Memory | Algorithm | Caching | Concurrency **Problem**: Brief explanation of the issue **Impact**: Why this matters (performance cost, resource usage, etc.) **Fix**: [Code example showing the optimized version]
Severity Levels
- •Critical: Causes crashes, severe memory leaks, or O(n³)+ complexity
- •High: Significant performance impact (O(n²), blocking operations, resource exhaustion)
- •Medium: Noticeable impact under load (redundant operations, suboptimal algorithms)
- •Low: Minor improvements (micro-optimizations, style improvements with perf benefit)
Language-Specific Checks
JavaScript/TypeScript
- •Array methods inside loops (map/filter/find in forEach)
- •Missing async/await causing blocking
- •Event listener leaks
- •Unbounded arrays/objects
Python
- •List comprehensions vs generator expressions for large data
- •Global interpreter lock considerations
- •Context manager usage for resources
- •N+1 query patterns
General
- •Premature optimization warnings (only flag if genuinely impactful)
- •Database query patterns (N+1, missing indexes)
- •I/O in hot paths