mavu
v0.2.4
Published
A CLI tool that estimates Big-O time complexity of JavaScript code using AST analysis.
Maintainers
Readme
mavu
A CLI tool that estimates Big-O time complexity of JavaScript code using static AST analysis.
Features
- Whole-file analysis: Finds loops (including nested), array-method iterations (
.map(),.forEach(), etc.), and recursion to estimate O(1), O(n), O(nᵏ), O(log n) or O(2ⁿ). - Per-function breakdown: Get a table of each function’s complexity.
- Explain mode: Shows a little ASCII tree of how your code’s complexity was derived.
- Batch & glob support: Run against single files, directories or globs (
src/**/*.js). - Multiple output formats: JSON, plain summary, ASCII table, or full explanation.
Installation
npm install -g mavuOr add it to your project:
npm install --save-dev mavuUsage
mavu [options] <file|glob>...Examples
Simple summary
mavu src/samples/fibRecursive.js
# src/samples/fibRecursive.js: O(2^n)Plain summary
mavu src/samples/logLoop.js --summary
# File: src/samples/logLoop.js
# Complexity: O(log n)JSON output
mavu src/samples/noLoops.js --json{
"file": "src/samples/noLoops.js",
"complexity": "O(1)",
"reason": ["No recursion/log detected; based on loop nesting.", "Max loop nesting depth: 0"]
}ASCII table
mavu src/samples/*.js --table┌──────────────────────────┬────────────┐
│ File │ Complexity │
├──────────────────────────┼────────────┤
│ noLoops.js │ O(1) │
│ logLoop.js │ O(log n) │
│ fibRecursive.js │ O(2^n) │
└──────────────────────────┴────────────┘Per-function breakdown
mavu src/samples/multiFunc.js --per-functionFunctions in src/samples/multiFunc.js
┌──────────┬────────────┐
│ Function │ Complexity │
├──────────┼────────────┤
│ foo │ O(n) │
│ bar │ O(n^2) │
│ baz │ O(n) │
└──────────┴────────────┘Explain & ASCII tree
mavu src/samples/logLoop.js --explainsrc/samples/logLoop.js → O(log n)
↳ Detected logarithmic loop (i *= 2)
└─ Max loop nesting depth: 1
Detected logarithmic loop (i *= 2)
Max loop nesting depth: 1CLI Options
| Flag | Description |
| -------------------- | --------------------------------------- |
| -J, --json | Emit JSON (object or array) |
| -S, --summary | Plain summary (File: / Complexity:) |
| -T, --table | ASCII table of files & complexities |
| -P, --per-function | Table of each function’s complexity |
| -E, --explain | Full reasoning + ASCII tree |
| -v, --version | Show version |
| -h, --help | Show this help message |
How It Works
Parsing Uses Acorn to parse your code to an AST.
Loop Analysis Walks
ForStatement/WhileStatement/ array-method calls to compute nesting depth and detect logarithmic strides.Recursion Analysis Builds a simple call graph to find self-calls, linear vs. tree vs. mutual recursion.
Decision Applies:
- Exponential if tree recursion or mutual recursion seen
- O(nᵈ) for maximum loop nesting depth
d - O(log n) if any loop doubles (or halves) its index
- O(n) if linear recursion or depth=1
- O(1) otherwise
License
MIT © hipuku
