Overview
Ever wondered if your JS/TS code is secretly harboring a performance monster? worstcase is your solution! This powerful tool automatically analyzes your JS/TS code and computes approximate Big O complexity for both time and space through static code analysis.
Motivation
The motivation is simple yet ambitious: to bring algorithmic analysis directly into the development workflow. Instead of manually reasoning through loops and recursive calls, or waiting for performance issues to surface in production, this analyzer examines your code structure and provides instant complexity estimates. It's like having a computer science professor looking over your shoulder, but one who never gets tired and works at the speed of light.
Features
- Automated Complexity Analysis: Computes Big O notation for time and space complexity
- Block-level Analysis: Granular complexity computation for each code block
- AST-Based Parsing: Uses Babel parser for accurate TypeScript/JavaScript/JSX code parsing
- No Pattern Matching: Pure algorithmic analysis without relying on pre-known patterns
- Conservative Estimates: Provides reasonable defaults for unknown code
- Built-in Method Knowledge: Knows complexity of basic Array/Object methods
Live Demo
Check the Monaco Editor integration demo:
View Repo |
Launch Demo
Quick Start
Installation
npm install @babel/parser worstcase
In a TypeScript project, add @babel/types
npm install @babel/types
Basic Usage
import { analyzeComplexity } from "worstcase";
const code = `
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
`;
const analysis = analyzeComplexity(code);
console.log(analysis.overall.time);
console.log(analysis.overall.space);
API Reference
analyzeComplexity(code: string, options?: Partial<WCOptions>): WCAnalysis
Analyzes JavaScript/TypeScript code and returns complexity information.
Parameters
code | string | JavaScript/TypeScript source code to analyze |
options | Partial<WCOptions> | undefined | Configure the analyzer |
Returns: WCAnalysis
type WCOptions = {
clean: boolean;
};
interface WCAnalysis {
overall: {
time: string;
space: string;
};
results: Array<{
type: string;
node: Node;
location: string;
time: string;
space: string;
}>;
}
Example Response
{
overall: {
time: "O(n^2)",
space: "O(1)"
},
results: [
{
type: "FunctionDeclaration",
location: "Line 2",
time: "O(n^2)",
space: "O(1)",
node: {...}
}
]
}
Limitations
This tool provides approximations, not perfect mathematical analysis. Current limitations:
- Dynamic behavior: Cannot analyze runtime-dependent complexity
- External dependencies: Unknown functions assumed to be
O(1)
- Complex algorithms: May not recognize advanced algorithmic patterns
- Halting problem: Cannot guarantee termination analysis
Architecture
The analyzer uses a multi-step approach:
- Parsing: Uses Babel parser to generate Abstract Syntax Tree (AST)
- Traversal: Visits each AST node with specific complexity rules
- Combination: Applies mathematical rules for combining complexities
- Simplification: Reduces to dominant terms in Big O notation
Use Cases
worstcase is perfect for:
- Helping developers understand the performance implications of their code
- Catching potential performance bottlenecks during development
- Serving as an educational tool for learning complexity analysis
- Giving instant feedback without requiring manual calculation
- Giving real-time complexity hints via IDE integration
Contributing
Thank you for checking out this awesome project.
Contributions are welcome!
Areas for improvement:
- Recursive pattern recognition: Advanced recurrence relation solving
Development Setup
Support
If you find this project useful, please consider giving it a star ⭐️ on GitHub!
License
Copyright (c) 2025-present Henry Hale.
MIT License - see LICENSE.txt file for details.