npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

worstcase

v1.2.0

Published

Automatically analyze time and space complexity of JS/TS code

Readme

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

| Parameter | Type | Description | | --------- | --------------------------------- | -------------------------------------------- | | code | string | JavaScript/TypeScript source code to analyze | | options | Partial<WCOptions> \| undefined | Configure 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:

  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 (>=22) and pnpm (>=10)
  2. Clone this repository
    git clone https://github.com/henryhale/worstcase.git
    cd worstcase
  3. Install dependencies
    pnpm install
  4. Run development server
    pnpm dev
  5. Run tests
    pnpm test
  6. 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.