Skill Development

Optimize

Profile and optimize performance bottlenecks

install path ~/.claude/skills/optimize/SKILL.md
command /optimize
performanceoptimizationprofilingspeedmemory
SKILL.md

Optimize Skill

You are a performance optimization expert. When this skill is invoked, profile the specified code and optimize its bottlenecks.

What This Skill Does

Identifies performance bottlenecks through analysis and profiling, then applies targeted optimizations to improve speed, memory usage, or both.

Step-by-Step Instructions

  1. Understand the performance problem. Clarify:

    • What is slow or using too much memory?
    • What is the expected vs actual performance?
    • When does it happen? (Large datasets, high concurrency, specific operations)
    • Are there any performance metrics or benchmarks already?
  2. Identify the hot path. Analyze the code to find where time is spent:

    • Read the code flow from entry point to completion
    • Look for O(n^2) or worse algorithms in loops
    • Identify I/O operations (database queries, file reads, API calls)
    • Check for unnecessary work (redundant computations, unused data fetching)
    • Look for synchronous blocking operations
  3. Profile if possible. Use available profiling tools:

    • Node.js: --prof flag, clinic.js, or console.time()
    • Python: cProfile, py-spy, or time.time()
    • Browser: Performance tab, Lighthouse
    • Add timing measurements around suspected bottlenecks
  4. Optimize database queries. Common fixes:

    • Add missing indexes for WHERE, JOIN, and ORDER BY columns
    • Replace N+1 queries with JOINs or batch loading
    • Select only needed columns instead of SELECT *
    • Add pagination for large result sets
    • Use query analysis (EXPLAIN ANALYZE) to verify improvements
    • Add caching for frequently-read, rarely-changed data
  5. Optimize algorithms and data structures.

    • Replace O(n^2) search with a Map/Set for O(1) lookup
    • Use binary search instead of linear search for sorted data
    • Avoid creating unnecessary intermediate arrays (use generators or streaming)
    • Pre-compute values that are used repeatedly
    • Use appropriate data structures (Set for uniqueness, Map for key-value lookup)
  6. Optimize I/O and concurrency.

    • Parallelize independent async operations with Promise.all()
    • Use streaming instead of loading entire files into memory
    • Add connection pooling for database connections
    • Batch API calls instead of making them one at a time
    • Add caching (in-memory, Redis) for expensive computations or external calls
    • Use pagination and lazy loading for large data sets
  7. Optimize frontend performance (if applicable).

    • Reduce bundle size (code splitting, tree shaking, dynamic imports)
    • Optimize images (compression, lazy loading, appropriate formats)
    • Minimize DOM operations and re-renders
    • Use virtualization for long lists
    • Add appropriate caching headers
  8. Verify the optimization. After each change:

    • Measure the new performance
    • Compare against the baseline
    • Run tests to ensure correctness is preserved
    • Check for memory leaks introduced by caching
  9. Report results:

## Optimization Report

### Before
- Metric: value (e.g., response time: 2.3s)

### Changes Made
1. Description of optimization and expected impact

### After
- Metric: new value (e.g., response time: 180ms)

### Tradeoffs
- Any increased complexity or resource usage

Guidelines

  • Measure before optimizing. Do not guess where the bottleneck is.
  • Optimize the biggest bottleneck first. An optimized hot path matters; an optimized cold path does not.
  • Do not sacrifice readability for micro-optimizations. Only optimize what is measurably slow.
  • Caching solves many problems but introduces cache invalidation complexity. Be deliberate.
  • Premature optimization is real. If it is fast enough, leave it alone.
  • Every optimization should be accompanied by a benchmark or measurement.
  • Keep files under 600 lines. If optimization adds complexity, refactor into smaller modules.
  • Document WHY an optimization was needed so future developers do not simplify it away.

Copy this into ~/.claude/skills/optimize/SKILL.md to use it as a slash command in Claude Code.

get crystl