An open API service indexing awesome lists of open source software.

https://github.com/henryhale/worstcase

🧐 Automatically analyze time and space complexity of JavaScript code without relying on pattern matching
https://github.com/henryhale/worstcase

big-o-analysis bigocomplexity bigonotation henryhale js space-time-complexity

Last synced: 4 months ago
JSON representation

🧐 Automatically analyze time and space complexity of JavaScript code without relying on pattern matching

Awesome Lists containing this project

README

          



worstcase


Automatic time and space complexity analyzer for JavaScript/TypeScript code



npm
GitHub
npm downloads


## 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](https://github.com/henryhale/worstcase-monaco-demo) |
[Launch Demo](https://henryhale.github.io/worstcase-monaco-demo)

## Quick Start

### Installation

```bash
# npm
npm install @babel/parser worstcase
```

In a TypeScript project, add `@babel/types`

```bash
npm install @babel/types
```

## Basic Usage

```js
import { 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)
```

## API Reference

### `analyzeComplexity(code: string, options?: Partial): WCAnalysis`

Analyzes JavaScript/TypeScript code and returns complexity information.

#### Parameters

| Parameter | Type | Description |
| --------- | --------------------------------- | -------------------------------------------- |
| `code` | `string` | JavaScript/TypeScript source code to analyze |
| `options` | `Partial \| undefined` | Configure the analyzer |

#### Returns: `WCAnalysis`

```typescript
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
}>;
}
```

#### Example Response

```js
{
overall: {
time: "O(n^2)",
space: "O(1)"
},
results: [
{
type: "FunctionDeclaration",
location: "Line 2",
time: "O(n^2)",
space: "O(1)",
node: {...}
}
// ... more results
]
}
```

## 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:

1. **Parsing**: Uses Babel parser to generate Abstract Syntax Tree (AST)
2. **Traversal**: Visits each AST node with specific complexity rules
3. **Combination**: Applies mathematical rules for combining complexities
4. **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

1. Install [Node.js](https://nodejs.org) (>=22) and [pnpm](https://pnpm.io/) (>=10)
2. Clone this repository
```bash
git clone https://github.com/henryhale/worstcase.git
cd worstcase
```
3. Install dependencies
```bash
pnpm install
```
4. Run development server
```bash
pnpm dev
```
5. Run tests
```bash
pnpm test
```
6. Build for production
```bash
pnpm build
```

## Support

If you find this project useful, please consider giving it a star ⭐️ on GitHub!

## License

Copyright (c) 2025-present [Henry Hale](https://github.com/henryhale/).

MIT License - see [LICENSE.txt](https://github.com/henryhale/worstcase/blob/master/LICENSE.txt) file for details.