---
roadcrew_template_name: "audit-performance.md"
roadcrew_template_type: "command"
execution_mode: "auto-execute"
roadcrew_template_version: "v1.0"
roadcrew_last_updated: "2025-10-25"
roadcrew_min_version: "1.5.0"
roadcrew_license: "See LICENSE file in .roadcrew folder"
roadcrew_copyright: "Copyright (c) 2025 North Star Holdings, LLC"
spdx_license_identifier: "LicenseRef-RoadcrewLicense-1.0"
---

# audit-performance

Automatically identify slow functions, memory leaks, and inefficient code patterns to optimize application performance.

## Usage

```bash
/audit-performance [--threshold ms] [--files pattern]
```

## What This Command Does

Performs comprehensive performance analysis including:
- **Function Profiling** - Identifies slow-running functions with execution times
- **Memory Analysis** - Detects memory leaks and unused allocations
- **Algorithmic Inefficiency** - Finds O(n²) loops, nested iterations, unoptimized patterns
- **Resource Usage** - Identifies excessive I/O, database queries, network calls
- **Optimization Recommendations** - Prioritized suggestions with impact estimates

Generates performance report with metrics and improvement priorities.

## Implementation Strategy

### Phase 1: Setup & Analysis

1. **Detect project type** and language
2. **Identify bottlenecks** through code analysis:
   - Function call complexity
   - Loop structures and nesting depth
   - Database query patterns
   - API call chains

3. **Load baseline metrics** (previous profile, if exists)

### Phase 2: Profiling & Scanning

1. **Static Analysis** - Identify patterns without execution:
   - N² loops and nested iterations
   - Unbounded data structures
   - Recursive calls without termination checks
   - Inefficient sorting/searching

2. **Code Pattern Detection**:
   - Eager loading vs lazy loading
   - Unnecessary data copies
   - Repeated calculations
   - Synchronous vs async opportunities

3. **Dependency Analysis**:
   - Heavy imports at module load
   - Bloated dependencies
   - Circular dependencies

### Phase 3: Report Generation

```markdown
# Performance Audit Report

**Scan Date**: {{DATE}}
**Project**: {{PROJECT_NAME}}
**Baseline**: {{PREVIOUS_SCAN_DATE}} ({{PREVIOUS_EXECUTION_TIME}}ms)

## Summary

- ⏱️ Slow Functions: N
- 🔴 Memory Leaks: N  
- 🟡 Algorithmic Issues: N
- 📊 Total Findings: N

## Critical Performance Issues

**Issue 1: [Title]**
- **Location**: `path/to/file.ts:123` - `functionName()`
- **Current Performance**: {{X}}ms / {{MEMORY}}MB
- **Expected Target**: {{X}}ms / {{MEMORY}}MB
- **Problem**: [Description]
- **Root Cause**: [Analysis]
- **Impact**: Affects {{X}} requests/day, saves {{Y}}ms if fixed
- **Recommended Fix**: [Specific optimization]
- **Effort**: 1-2 hours / 2-4 hours / 4+ hours
- **Performance Gain**: {{X}}% faster

## Improvement Priority

### Phase 1 (Critical - Blocks Scalability)
- [Issue affecting 100+ requests/day]
- [Memory leak causing OOM errors]

### Phase 2 (High - Noticeable User Impact)
- [Issue affecting user experience]
- [Database queries >500ms]

### Phase 3 (Medium - Performance Optimization)
- [Issue affecting background jobs]

### Phase 4 (Low - Nice to Have)
- [Code hygiene improvements]

## Metrics & Trends

- Previous scan: {{PREV_DATE}} ({{N}}ms avg)
- Current scan: {{DATE}} ({{N}}ms avg)
- Trend: {{+/-}}{{N}}% vs previous
- Optimization Velocity: {{N}} issues resolved
```

### Phase 4: Integration

- Create GitHub issues for critical/high findings
- Add to performance tracking dashboard
- Generate recommendations for next sprint

## Output Format Example

```bash
$ /audit-performance --threshold 500

⚡ Performance Audit: roadcrew-internal

Scan Time: 1.2 seconds | Functions Analyzed: 342 | Threshold: >500ms

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 FINDINGS SUMMARY
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Slow Functions: 7
Memory Leaks: 2
Algorithmic Issues: 12
Total: 21

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🚀 CRITICAL PERFORMANCE ISSUES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

[Issue 1]
Location: scripts/core/github-issue-creator.ts:145
Function: createIssuesInBatch()
Current: 2300ms | Target: 1000ms | Gain: 1300ms (56%)
Problem: N² complexity in issue linking loop
Recommendation: Use Map for O(1) lookups
Effort: 2-3 hours

[Issue 2]
...

📊 Full report: config/reports/performance-audit-2025-10-29.md
```

## Flags & Options

```bash
--threshold ms                  # Report functions slower than threshold (default: 100ms)
--files pattern                 # Analyze specific files only
--compare previous              # Compare to previous scan
--show-all                      # Show all findings (not just critical)
--create-issues                 # Create GitHub issues for critical findings
```
