Code Complexity Analyzer
Analyze cyclomatic and cognitive complexity of JavaScript and TypeScript code. Per-function metrics with maintainability index.
| Function | Line | Cyclomatic | Cognitive | Maint. Index | Nesting | LOC |
|---|
About this tool
Code complexity analyzer measures how hard your JavaScript and TypeScript functions are to understand, test, and maintain. It computes cyclomatic complexity (branching paths), cognitive complexity (how difficult the code is to read), maintainability index (a 0–100 score with letter grades), nesting depth, and lines of code — all per function.
Example input: a function with nested if/else, a for loop, and a switch statement might score cyclomatic 8 and cognitive 12. Example output: a table listing each function with color-coded scores — green for low complexity, yellow for moderate, red for high.
Use it during code review to flag functions that need refactoring: a cyclomatic complexity above 10 or cognitive above 15 usually signals a function trying to do too much.
FAQ
What is cyclomatic complexity? ›
Cyclomatic complexity (McCabe) counts the number of linearly independent paths through a function. It starts at 1 and adds 1 for every if, for, while, case, catch, &&, ||, ??, and ternary operator. Scores under 10 are low risk; 10–20 is moderate; above 20 is high risk and hard to test.
How is cognitive complexity different from cyclomatic? ›
Cognitive complexity (SonarQube model) penalizes nesting: an if inside a for inside another if scores higher than three sequential ifs. It also accounts for structural breaks like break-to-label and recursion-like patterns. It aims to measure how hard code is to <em>read</em>, not just how many paths exist.
What does the Maintainability Index grade mean? ›
MI combines Halstead Volume (operator/operand counts), cyclomatic complexity, and lines of code into a 0–100 score. Grade A (≥85) is very maintainable; B (65–84) is good; C (40–64) is moderate; D (20–39) is low; F (<20) needs refactoring. The formula is based on the SEI (Software Engineering Institute) standard.
Does this work with TypeScript? ›
Yes. The parser treats type annotations, interfaces, enums, and generics as part of the surrounding code without penalizing them. Functions with typed parameters are analyzed the same way as plain JavaScript functions.