New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

worstcase

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

worstcase

Automatically analyze time and space complexity of JS/TS code

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

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 | Launch Demo

Quick Start

Installation

# npm
npm install @babel/parser worstcase

In a TypeScript project, add @babel/types

npm install @babel/types

Basic Usage

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<WCOptions>): WCAnalysis

Analyzes JavaScript/TypeScript code and returns complexity information.

Parameters

ParameterTypeDescription
codestringJavaScript/TypeScript source code to analyze
optionsPartial<WCOptions> | undefinedConfigure the analyzer

Returns: WCAnalysis

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

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

  • 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

  • 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
    

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.

Keywords

big-o

FAQs

Package last updated on 26 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts