As codebases scale under aggressive product cycles, functions can easily become bloated with deeply nested loops and excessive conditional branches. This increases technical debt and makes unit testing much harder.
To help developers audit their code hygiene instantly and securely, we built a JS Code Complexity Analyzer running completely client-side. Here is a technical breakdown of how we calculated McCabe's Cyclomatic Complexity, mapped maximum nesting depths, and approximated the industry-standard Maintainability Index in the browser.
1. The Mathematics of Cyclomatic Complexity
Developed by Thomas J. McCabe in 1976, Cyclomatic Complexity measures the number of linearly independent execution paths through a program's source code.
Rather than loading a heavy Abstract Syntax Tree (AST) parser in the browser, our engine performs a highly efficient, regex-based token scan. The algorithm starts with a baseline complexity score of 1. For each decision node or branching point detected in the code, the score increases by 1.
We scan lines for the following branching patterns (while ignoring matches found inside comments):
- Conditional branches:
if,case - Loop structures:
for,while - Error handling:
catch - Logical operators:
&&,|| - Ternary operators:
?
A higher score indicates more independent paths, warning developers that the function requires more unit tests to cover all execution conditions.
2. Scanning Maximum Nesting Depth
Nesting depth measures how deeply control structures are layered inside one another. High complexity with flat nesting is often manageable, but deeply nested code is notoriously hard to read.
Our engine calculates the maximum nesting depth by scanning the brace-balance ({ and }) of the code block sequentially. It tracks the current nesting depth in real-time, recording the maximum depth reached during the lexical scan.
let currentDepth = 0;
let maxDepth = 0;
for (let i = 0; i < code.length; i++) {
if (code[i] === '{') {
currentDepth++;
if (currentDepth > maxDepth) {
maxDepth = currentDepth;
}
} else if (code[i] === '}') {
currentDepth = Math.max(0, currentDepth - 1);
}
}
3. Approximating the Maintainability Index
The Maintainability Index is an industry-standard metric (ranging from 0% to 100%) indicating how easy the code is to support and modify. The classic formula balances Halstead Volume, Cyclomatic Complexity, and Source Lines of Code (SLOC).
Our engine calculates a highly reliable approximation of this index using a logarithmic scale:
$$\text{Maintainability Index} = 171 - (3.5 \times \ln(\text{Complexity})) - (0.23 \times \text{Complexity}) - (16.2 \times \ln(\text{SLOC}))$$
- Excellent Maintainability: Scores above 75% (low complexity, short, clean).
- Moderate Maintainability: Scores between 50% and 75%.
- Low Maintainability: Scores below 50% (high complexity, deep nesting, long lines).
4. Interactive Sandbox & Security Hygiene
The tool displays these metrics on a responsive dashboard:
- McCabes Complexity Score: Classified by risk ratings (Low Risk < 10, Moderate Risk 10–15, High Risk > 15).
- Nesting Depth Counter: Flags blocks nested too deeply.
- SLOC Breakdown: Tracks actual Source Lines of Code, comments, and blank spacer lines.
Transmitting proprietary enterprise code, server router functions, or security scripts to external servers poses a severe privacy risk. By executing all static analysis, regex parsing, and index math locally inside your browser's private sandbox memory, your configurations remain completely secure.
Test your custom JavaScript routines, analyze nested loops, and check your complexity scores:
👉 Test the Live Tool: https://tools.kandz.me/code-complexity
Top comments (0)