# Performance Profiling Skill

## Purpose
Identify and fix performance bottlenecks in Node.js and browser applications.

## Common Performance Issues

### N+1 Query Problem
Executing one query per item instead of batching:
- Use JOINs or IN clauses to batch database queries
- Use DataLoader pattern for GraphQL resolvers

### Memory Leaks
- Unbounded caches or arrays that grow indefinitely
- Event listeners that are never removed
- Closures holding references to large objects
- Fix: implement LRU cache, WeakRef, cleanup on unmount

### Algorithmic Complexity
- O(n^2) loops that can be O(n) with a Set or Map
- Sorting inside loops
- String concatenation in loops (use array.join)

### Blocking the Event Loop
- Synchronous file I/O in request handlers
- CPU-intensive computation on the main thread
- Fix: use worker threads, streaming, or chunked processing

## Profiling Tools
- Node.js: --inspect flag + Chrome DevTools
- Memory: process.memoryUsage(), heap snapshots
- CPU: console.time/timeEnd, Performance API
- Database: EXPLAIN ANALYZE for SQL queries