Complexity Lens
Inline Big-O time & space complexity estimates for your functions, computed locally via static AST analysis. No data leaves your machine.
Supported languages: JavaScript, TypeScript, JSX/TSX, Python, Java, Go, Rust.
Supported editors: VS Code, Cursor (works as-is), with adapters for IntelliJ and others planned.
How it works
The extension parses your source with tree-sitter, walks each function body, and rates it on two axes:
| Indicator |
Time complexity |
Space complexity |
| Good |
O(1), O(log n) |
O(1) |
| Average |
O(n), O(n log n) |
O(log n), O(n) |
| Slow / Heavy |
O(n²) and worse |
O(n log n) and worse |
Ratings appear inline using VS Code's built-in product icons (Codicons) — pass-filled for Good, warning for Average, error for Slow.
Usage
Complexity Lens has two modes. Pick the one that fits your workflow in Settings → Complexity Lens → Mode.
Automatic mode (default)
When you open any supported file, every top-level function gets an inline label above it:
Good · O(n) time · O(1) space
function sumArray(arr) {
...
}
- Annotations refresh whenever the file changes.
- Hover any function to see a detailed breakdown with confidence and rationale.
- Run Complexity Lens: Analyze Current File from the command palette to force a re-analysis.
- Run Complexity Lens: Clear Annotations to hide everything until the next file open.
Automatic mode only annotates files that are open in the editor. Files sitting on disk are never read.
Manual mode (selection-only)
If you find inline labels noisy, switch the mode to manual. Nothing is annotated automatically — you opt in per piece of code:
- Select the function or block you want analyzed.
- Right-click the selection.
- Choose Analyze Complexity.
The result appears as a one-line indicator next to the function header inside your selection. Hover it for the full breakdown. Edit the file or run Complexity Lens: Clear Annotations to remove it.
You can also bind complexityLens.analyzeSelection to a shortcut (e.g. Ctrl+Alt+C) via File → Preferences → Keyboard Shortcuts.
Switching modes
Run Complexity Lens: Toggle Automatic / Manual Mode from the command palette, or change complexityLens.mode in settings.
Settings
| Setting |
Default |
What it does |
complexityLens.mode |
automatic |
automatic annotates every open file; manual only runs on right-click. |
complexityLens.showSpace |
true |
Show the space-complexity column alongside time. |
complexityLens.confidenceThreshold |
low |
Hide estimates below this confidence (low / medium / high). |
What it can and can't see
Complexity Lens uses pattern-based heuristics, not symbolic execution. It does well on:
- Standard loop nesting (
O(n), O(n²), O(n³))
- Halving loops and binary-search patterns (
O(log n))
- Built-in sort calls (
Array.sort, sorted(), Arrays.sort, sort.Slice, .sort_unstable)
- Direct recursion, including divide-and-conquer
- Common linear allocations (
new Array(n), [0]*n, make([]T, n), vec![…; n])
It is conservative — and labelled low confidence — for:
- Indirect recursion across files
- Loops with non-obvious bounds (
while (cond) where cond involves multiple variables)
- Dynamic programming with memoization (often reported as the un-memoized cost)
- Calls into third-party libraries
When the rationale says "branching factor unknown" or "loop bounds unclear", trust your own judgment over the label.
Architecture (for contributors)
The extension is built around three boundaries that make it easy to extend:
┌──────────────────┐ ┌───────────────────┐ ┌──────────────────────┐
│ Language │───▶│ Heuristic engine │───▶│ Editor presentation │
│ analyzer │ │ (Big-O inference)│ │ (CodeLens / Hover / │
│ (per language) │ │ │ │ selection deco) │
└──────────────────┘ └───────────────────┘ └──────────────────────┘
IComplexityAnalyzer FunctionShape → VS Code today,
Complexity IntelliJ / Zed / CLI
tomorrow
- Add a language: implement
IComplexityAnalyzer (or extend BaseAnalyzer with a LanguageRules table) and call registry.register(...) in extension.ts.
- Add an editor: re-use
core/ and analyzers/ unchanged; write a new presentation layer that talks to that editor's API. Only src/providers/ and src/extension.ts are VS Code-specific.
Build & run locally
npm install
npm run compile
# Then press F5 in VS Code to launch an Extension Development Host.
License
MIT.