# Performance Patterns

Optimize app performance by applying database indexing, API compression, memory streaming, and other proven techniques. Deliverables include optimized code and performance improvement metrics.

**Canonical URL:** https://skillshunter.net/skills/discovered-9db3970ceae9f4205da5dfddb339ada5033c41bd38b036c1

**JSON URL:** https://skillshunter.net/api/public/v1/skills/discovered-9db3970ceae9f4205da5dfddb339ada5033c41bd38b036c1

**Markdown URL:** https://skillshunter.net/skills/discovered-9db3970ceae9f4205da5dfddb339ada5033c41bd38b036c1.md

**Instructions download URL:** https://skillshunter.net/skills/discovered-9db3970ceae9f4205da5dfddb339ada5033c41bd38b036c1/download

**Category:** Software & Development

**Source URL:** https://github.com/0x57-1/start-vibing-stacks/blob/6e398b34c4dfe73ac8f809216b218856b8e02ff4/stacks/_shared/skills/performance-patterns/SKILL.md

**Source commit:** 6e398b34c4dfe73ac8f809216b218856b8e02ff4

**Listing approved at:** 2026-12-03

**Created at:** 2026-12-02T09:11:35.000000Z

**Updated at:** 2026-12-03T16:20:08.000000Z

## Catalog information

The following descriptions are catalog information. Listing approval does not mean the skill has been tested or is safe for every environment.

### Editor's note

Measure performance first using profiling tools to identify bottlenecks. Ensure environment supports Redis for caching and Node.js or PHP for specific implementations.

### When to use it

- Optimize slow database queries by adding indexes and using eager loading
- Reduce API response size with compression and selective field selection
- Improve memory usage by streaming large files and releasing references

## Skill instructions

---
name: performance-patterns
version: 1.0.0
---

# Performance Patterns

**Invoke when app is slow or needs optimization.**

## Universal Patterns

### Database
- **Index** frequently queried columns
- **Eager load** relationships (avoid N+1)
- **Paginate** large result sets
- **Cache** expensive queries (Redis, 15min TTL)
- **Chunk** batch operations (100-500 per batch)

### API
- **Compress** responses (gzip/brotli)
- **Cache** with ETags / Last-Modified
- **Paginate** list endpoints
- **Select only needed fields** (no `SELECT *`)

### Memory
- **Stream** large files (don't load into memory)
- **Release** references in loops
- **Monitor** memory usage in long-running processes

## PHP Specific

```php
// ❌ N+1 problem
$users = User::all();
foreach ($users as $user) {
    echo $user->posts->count(); // Query per user!
}

// ✅ Eager loading
$users = User::with('posts')->get();

// ✅ Chunked processing
User::where('active', true)->chunkById(100, function ($users) {
    foreach ($users as $user) {
        ProcessUserJob::dispatch($user);
    }
});
```

## React Specific

```tsx
// ❌ New object every render
<Component style={{ color: 'red' }} />

// ✅ Memoized
const style = useMemo(() => ({ color: 'red' }), []);

// ✅ Lazy loading
const HeavyChart = lazy(() => import('./HeavyChart'));

// ✅ Virtualization for long lists
import { useVirtualizer } from '@tanstack/react-virtual';
```

## Rules

1. **MEASURE FIRST** — don't optimize blindly
2. **PROFILE** — identify the actual bottleneck
3. **ONE CHANGE AT A TIME** — isolate improvements
4. **BENCHMARK** — before and after metrics

