Optimizing Code
Testing & Qualityoptimizing-codeOptimizing Code is an Agent Skill for testing & quality published in the majiayu000/claude-skill-registry repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.
Improve code performance without changing behavior. Use when code fails latency/throughput requirements. Covers profiling, caching, and algorithmic optimization.
QUICK START
How to use this skill
Bring this guide into your coding agent with a prompt tailored to the tool you use.
- Open your project in Codex.
- Copy and paste the prompt below.
- Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/majiayu000/claude-skill-registry/blob/HEAD/skills/data/optimizing-code/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/optimizing-code, but wait for my approval before writing files or running scripts.
Copying this prompt does not install or run the skill. Review third-party files before use.
Optimizing Code
The Optimization Hat
When optimizing, you improve performance without changing behavior. Always measure before and after.
Golden Rules
- Measure First: Never optimize without a benchmark
- Profile Before Guessing: Find the actual bottleneck
- Optimize the Right Thing: Focus on the critical path
- Measure After: Verify the optimization worked
Workflows
- Benchmark: Establish baseline performance metrics
- Profile: Identify the actual bottleneck
- Hypothesize: What optimization will help?
- Implement: Make the change
- Measure: Verify improvement
- Document: Record the optimization and results
Common Optimizations
Algorithm Complexity
- Replace O(n²) with O(n log n) or O(n)
- Use appropriate data structures (Set for lookups, Map for key-value)
Caching
// Memoization
const cache = new Map<string, Result>();
function expensiveCalculation(input: string): Result {
if (cache.has(input)) {
return cache.get(input)!;
}
const result = /* expensive work */;
cache.set(input, result);
return result;
}
Database Queries
- Add indexes for frequently queried columns
- Avoid N+1 queries (use eager loading)
- Use pagination for large result sets
Memory
- Avoid creating unnecessary objects in loops
- Use streaming for large files
- Release references when done
Profiling Tools
# Node.js
node --prof app.js
node --prof-process isolate-*.log
# Python
python -m cProfile -s cumtime script.py
# Go
go test -bench=. -cpuprofile=cpu.prof
go tool pprof cpu.prof
Anti-Patterns to Avoid
- Premature optimization (no benchmark)
- Micro-optimizations (negligible impact)
- Optimizing cold paths
- Sacrificing readability for minor gains