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.
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.
- 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
Check the Monaco Editor integration demo: View Repo | Launch Demo
# npm
npm install @babel/parser worstcaseIn a TypeScript project, add @babel/types
npm install @babel/typesimport { analyzeComplexity } from "worstcase";
// Example: Analyzing a bubble sort implementation
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); // O(n^2)
console.log(analysis.overall.space); // O(1)Analyzes JavaScript/TypeScript code and returns complexity information.
| Parameter | Type | Description |
|---|---|---|
code |
string |
JavaScript/TypeScript source code to analyze |
options |
Partial<WCOptions> | undefined |
Configure the analyzer |
type WCOptions = {
clean: boolean; // Whether or not to drop coefficients - default: true
};
interface WCAnalysis {
overall: {
time: string; // Overall time complexity (e.g., "O(n^2)")
space: string; // Overall space complexity (e.g., "O(1)")
};
results: Array<{
type: string; // AST node type
node: Node; // Babel AST node: from @babel/types
location: string; // Code location
time: string; // Time complexity
space: string; // Space complexity
}>;
}{
overall: {
time: "O(n^2)",
space: "O(1)"
},
results: [
{
type: "FunctionDeclaration",
location: "Line 2",
time: "O(n^2)",
space: "O(1)",
node: {...}
}
// ... more results
]
}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
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
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
Thank you for checking out this awesome project. Contributions are welcome!
Areas for improvement:
- Recursive pattern recognition: Advanced recurrence relation solving
- Install Node.js (>=22) and pnpm (>=10)
- Clone this repository
git clone https://github.com/henryhale/worstcase.git cd worstcase - Install dependencies
pnpm install
- Run development server
pnpm dev
- Run tests
pnpm test - Build for production
pnpm build
If you find this project useful, please consider giving it a star ⭐️ on GitHub!
Copyright (c) 2025-present Henry Hale.
MIT License - see LICENSE.txt file for details.