Analyze code for performance issues and suggest optimizations. Use when users ask to "optimize this code", "find performance issues", "improve performance", "check for memory leaks", "review code efficiency", or want to identify bottlenecks, algorithmic improvements, caching opportunities, or concurrency problems.
Resources
1Install
npx skillscat add luongnv89/skills/code-optimizer Install via the SkillsCat registry.
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
0. Create Feature Branch
Before making any changes:
- Check the current branch - if already on a feature branch for this task, skip
- Check the repo for branch naming conventions (e.g.,
feat/,feature/, etc.) - Create and switch to a new branch following the repo's convention, or fallback to:
feat/optimize-<target>- Example:
feat/optimize-api-handlers
- Example:
1. Analysis
- Read the target code file(s) or directory
- Identify language, framework, and runtime context (Node.js, CPython, browser, etc.)
- Analyze for each priority category in order
- For each issue found, estimate the performance impact (e.g., "reduces API response from ~500ms to ~50ms")
- Report findings sorted by severity (Critical first)
2. Apply Fixes
- Present the optimization report to the user
- On approval, apply fixes starting with Critical/High severity
- Run existing tests after each change to verify no regressions
- If no tests exist, warn the user before applying changes
Response Format
For each issue found:
### [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
Error Handling
No obvious performance issues found
Solution: Report that the code is already well-optimized. Suggest profiling with runtime tools (e.g., perf, Chrome DevTools, py-spy) to find runtime-specific bottlenecks.
Target file is too large (>2000 lines)
Solution: Ask the user to specify which functions or sections to focus on. Analyze the most performance-critical paths first.
Optimization breaks existing tests
Solution: Revert the change immediately. Re-examine the optimization and adjust the approach to preserve existing behavior.