agent-complexity-optimizer

A performance doctor for your codebase. It finds inefficient algorithms and bottlenecks (O(n^2) loops, N+1 queries, sequential awaits, collections copied on every iteration), ranks the functions most likely to be slow, and helps your AI agent confirm each lead with profilers and growth benchmarks before touching code.
Works as a skill/plugin for 13 AI coding agents, or standalone via a dependency-free Python CLI.
Extended from codex-complexity-optimizer by Kappaemme. See CREDITS.md for full attribution and a breakdown of what this project adds.
Demo

Full-quality video: demo/promo.mp4
Given this code:
def find_duplicates(users, transactions):
duplicates = []
for t in transactions:
for u in users: # O(n*m) nested scan
if u["id"] == t["user_id"]:
duplicates.append(t)
return duplicates
def get_user_orders(user_ids, db):
results = []
for uid in user_ids:
order = db.query(f"SELECT * FROM orders WHERE user_id = {uid}") # N+1
results.append(order)
return results
export async function notifyAll(users: User[], mailer: Mailer) {
for (const user of users) {
await mailer.send(user);
}
}
export function indexById(items: Item[]) {
return items.reduce((acc, item) => ({ ...acc, [item.id]: item }), {});
}
The scanner produces (trimmed):
# Complexity Hotspots
**Density: 25.8 points per 1,000 lines** · 2 files, 21 lines scanned · 4 findings
## Top functions
| # | Function | Location | Score | Findings |
|---|-------------------|-----------------|-------|------------------------|
| 1 | `find_duplicates` | `example.py:4` | 10.0 | nested-loop |
| 2 | `get_user_orders` | `example.py:12` | 8.0 | io-or-query-in-loop |
| 3 | `notifyAll` | `notify.ts:3` | 4.2 | await-in-loop |
| 4 | `indexById` | `notify.ts:8` | 3.6 | quadratic-accumulation |
## Findings
### 1. HIGH nested-loop · `example.py:4` · `find_duplicates`
for u in users: # O(n*m) nested scan
- Finding: Loop over an independent collection inside another loop: O(n*m) or worse.
- Suggestion: Index the inner collection once (map/set/grouping), or use sort + two pointers / sweep line for pairwise work.
- score 10.0 · confidence high · loop depth 2
...
What It Detects
| Pattern | Severity | Example |
|---|
io-or-query-in-loop | High | db.query(), repo.findOne(), fetch() per element (N+1) |
await-in-loop | High | for (const u of users) await send(u), independent calls run one by one |
quadratic-accumulation | High | reduce((acc, x) => ({...acc, ...})), all = all.concat(page), df = pd.concat([df, row]) |
nested-loop | High | for a in A: for b in B over independent collections, O(n*m) |
sort-in-loop | High | Re-sorting a growing list every iteration |
string-concat-in-loop | Medium | s += piece in Java, Kotlin, C#, Go (immutable strings) |
list-shift-in-loop | Medium | pop(0), shift(), remove(0) inside a loop |
dataframe-row-loop | Medium | df.iterrows(), df.apply(f, axis=1) |
membership-in-loop | Medium | x in list, .includes(), .contains(), .index() inside a loop |
deep-copy-in-loop | Medium | deepcopy, JSON.parse(JSON.stringify(x)) per element |
repeated-scan | Medium | filter() / map() builtins re-run per iteration (Python) |
regex-compile-in-loop | Medium | new RegExp(), Pattern.compile(), regexp.MustCompile() per element |
render-derived-work | Medium | .filter() / .sort() in a React component body without useMemo |
Precision work so leads stay trustworthy on real repos: