---
name: performance-review
description: Reviews code for performance problems such as N+1 queries, work inside hot loops, unbounded memory growth, missing indexes, blocking I/O on the request path and chatty network calls. Use when the user asks why code is slow or to review a change for performance.
---

# Performance Review

Find the performance problems that matter at realistic scale and ask for measurements before recommending micro-optimizations.

## 1. Establish the context

Before reading code, find out:

- What is slow or at risk: latency of one request, throughput, memory, startup, a batch job, a UI frame?
- What scale is realistic: rows in tables, requests per second, size of inputs, number of users?
- Is there a measurement already (profiler output, slow query log, APM trace, benchmark)? If yes, start from it.

If there is no number and the user claims "it is slow", ask for a reproduction or add a cheap measurement first (timing log around the suspect, `EXPLAIN`, a profiler run). Do not guess.

## 2. Look for the usual big wins

Work from the largest cost down. These beat any micro-optimization:

- N+1 queries: a query inside a loop over results of another query, or lazy-loaded relations accessed per item. Fix with a join, `IN (...)` batch, or eager loading.
- Missing indexes: columns used in `WHERE`, `JOIN`, `ORDER BY` without an index; functions applied to indexed columns; leading wildcard `LIKE '%x'`. Verify with the database's explain tool.
- Fetching too much: `SELECT *`, loading whole tables to filter in memory, no pagination or limit, returning huge payloads.
- Work inside hot loops: regex compilation, JSON parsing, config reads, object construction or sorting repeated per iteration instead of once outside.
- Quadratic behavior: nested loops over collections, `list.contains` or array `includes` inside a loop (use a set or map), string concatenation in a loop on large data.
- Unbounded memory: collections, caches or queues that only grow; reading a whole file or response into memory instead of streaming; listeners or timers never removed.
- Blocking I/O on the request path: synchronous file, network or CPU-heavy work in an event loop or request handler; calls that should be queued or moved to a background job.
- Chatty network: many small sequential calls where one batch call works; independent calls that could run in parallel; no timeouts or connection reuse.
- Missing caching of stable, expensive results, or caching without invalidation or a size bound.
- Frontend: re-rendering large lists without keys or virtualization, large bundles loaded eagerly, layout thrash.

## 3. Verify, do not assume

For each suspect, state the cost model: "this runs once per row, rows are about 10k, each call is about 2 ms, so about 20 s." If you cannot estimate, mark the finding as unverified and propose the exact measurement.

```bash
# examples of cheap measurements
time ./run-job.sh
python -X importtime app.py 2> imports.log
node --cpu-prof script.js
psql -c "EXPLAIN (ANALYZE, BUFFERS) SELECT ..."
```

Run measurements only on the user's dev data and never against production without asking.

## 4. Output format

```
Summary: <what was reviewed, the one biggest risk>

[high] path/to/file.js:57 - N+1 query in order listing
  Cost: <estimate: calls x time at realistic size>
  Evidence: <measured / read from code / unverified>
  Fix: <specific change, with the expected effect>

[medium] ...
[low] ...

Measurements to take: <what to run to confirm or rule out unverified findings>
Leave alone: <things that look slow but are not on a hot path>
```

## Pitfalls

- Do not micro-optimize (loop style, `for` vs `map`, small allocations) without a profile showing it matters.
- Do not trade readability for a speedup nobody can measure.
- Do not add caching as a first answer; remove the redundant work first.
- Do not change query semantics (ordering, null handling) while rewriting for speed; check results are identical.
- Do not forget the write side: new indexes slow inserts and take disk; say so.
- Before and after numbers belong in the report if you changed anything; if you did not measure, say that explicitly.

Source: [AnyModel skills catalog](https://anymodel.org/en/skills) · License: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
