Optimize

From InugamiDev/ultrathink-oss · View interactive version

Optimize is an AI agent skill from the GitHub repository InugamiDev/ultrathink-oss. Performance optimization workflow -- profile, identify bottlenecks, apply targeted improvements, verify gains

How to use this skill

Skill file

Optimize Skill

Purpose

Systematically identify and resolve performance bottlenecks using a measure-first approach. This skill prevents the most common optimization mistake: guessing where the problem is and optimizing the wrong thing.

The law of optimization: Measure first. Optimize the bottleneck. Measure again. Repeat.

Never optimize based on intuition alone. Developers are notoriously bad at predicting where performance problems live.

Workflow

Phase 1: Define the Target

  1. Identify what to optimize -- Be specific:
  1. Set the performance budget -- What is "good enough"?
  1. Establish the baseline -- Measure current performance before changing anything. Record exact numbers, not impressions.

Phase 2: Profile and Analyze

  1. Identify the profiling approach based on the target:

| Target | Profiling Approach |

|--------|-------------------|

| API latency | Add timing logs, trace request lifecycle |

| Database | Analyze query plans (EXPLAIN), check indexes |

| Frontend load | Analyze bundle (webpack-bundle-analyzer), check network waterfall |

| Render perf | Check component re-renders, identify expensive renders |

| Memory | Heap snapshots, allocation tracking |

| Build time | Build profiling, dependency analysis |

  1. Read the code in the hot path -- the execution path that matters for the target metric.
  1. Identify bottlenecks -- Look for these common performance problems:

Backend / General:

Frontend specific:

  1. Rank bottlenecks by impact -- Which bottleneck, if resolved, would produce the largest improvement? Start there.

Phase 3: Apply Optimizations

  1. Apply optimizations one at a time, measuring after each:

Common optimization patterns:

| Problem | Optimization | Trade-off |

|---------|-------------|-----------|

| N+1 queries | Batch query / JOIN | Query complexity |

| Missing index | Add database index | Write performance, storage |

| Sequential I/O | Promise.all / parallel | Error handling complexity |

| Redundant computation | Memoization / caching | Memory, staleness |

| Large payload | Field selection, pagination | API complexity |

| Large bundle | Dynamic import, tree-shaking | Loading states, code complexity |

| Unnecessary re-renders | React.memo, useMemo, useCallback | Code complexity |

| Unoptimized images | Next/Image, WebP, lazy loading | Build complexity |

| No caching | HTTP cache, in-memory cache, CDN | Invalidation complexity |

| Blocking computation | Web Worker, server-side | Architecture complexity |

  1. For each optimization:
  1. Stop when the budget is met or when remaining optimizations have diminishing returns.

Phase 4: Verification

  1. Run the full test suite -- Optimizations must not break functionality.
  1. Measure final performance -- Compare against baseline.
  1. Check for regressions in other dimensions:
  1. Produce the optimization report using the template below.

Optimization Report Template

# Optimization Report

## Target
**Metric**: [what was optimized]
**Baseline**: [measurement before]
**Budget**: [target]
**Result**: [measurement after]
**Improvement**: [percentage or absolute]

## Bottlenecks Identified
| # | Bottleneck | Impact | Location |
|---|-----------|--------|----------|
| B1 | [description] | [estimated impact] | [file:line] |
| B2 | [description] | [estimated impact] | [file:line] |

## Optimizations Applied
### O1: [Name]
**Bottleneck**: B1
**Change**: [description]
**Before**: [measurement]
**After**: [measurement]
**Trade-off**: [what was given up]
**Files changed**: [list]

### O2: [Name]
...

## Optimizations Considered but Not Applied
- [optimization]: [reason -- diminishing returns, too complex, etc.]

## Performance Summary
| Metric | Baseline | After | Change |
|--------|----------|-------|--------|
| [primary metric] | [value] | [value] | [improvement] |
| [secondary metric] | [value] | [value] | [change] |

## Verification
- [ ] All tests pass
- [ ] No regressions in other metrics
- [ ] Performance gains verified in representative conditions

## Maintenance Notes
[Any ongoing considerations -- cache invalidation, index maintenance, etc.]

Usage

General optimization

/optimize The /api/users endpoint takes 3 seconds to respond

Frontend performance

/optimize Reduce the initial page load time -- Lighthouse score is 45

Specific metric

/optimize Reduce the JavaScript bundle size below 150KB

Database optimization

/optimize The product search query is slow with 1M+ rows

Examples

Example: API endpoint optimization

Baseline: /api/orders responds in 2.3s

Profiling: Found N+1 query -- fetching customer for each order individually

Optimization: Replace loop of findCustomer(order.customerId) with single findCustomers(customerIds) using IN clause

Result: Response time dropped to 180ms (92% improvement)

Trade-off: Slightly more complex query code

Example: Bundle size optimization

Baseline: Initial JS bundle is 450KB

Profiling: moment.js (300KB) used for one formatDate() call

Optimization: Replace with date-fns/format (2KB) or native Intl.DateTimeFormat

Result: Bundle reduced to 155KB (66% reduction)

Trade-off: None meaningful

Example: Render optimization

Baseline: List of 500 items causes 2s freeze on filter change

Profiling: Every item re-renders on filter change because list component re-creates item objects

Optimization: Virtualize the list (react-window), memoize item components, stabilize references

Result: Filter change is instant, only visible items render

Trade-off: Added dependency, slightly more complex list component

Guidelines

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.