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

best-fit-strip-pack

v1.1.0

Published

A TypeScript implementation of the online best-fit algorithm for the 2D rectangular strip packing problem.

Readme

Best-Fit Strip Pack

NPM Version Coverage Status

A TypeScript implementation of the online best-fit algorithm for the 2D rectangular strip packing problem. The strip packing problem involves packing rectangles of varying dimensions into a strip of fixed width and infinite height, minimizing the total height used.

The algorithm places each rectangle in the position that minimizes the increase to the overall strip height, processing items sequentially in insertion order without pre-sorting. This makes it suitable for real-time scenarios where rectangles arrive one by one.

Based on the best-fit heuristic described in "The best-fit heuristic for the rectangular strip packing problem: An efficient implementation and the worst-case approximation ratio" by Shinji Imahori and Mutsunori Yagiura.

Features

  • Online Best-Fit Heuristic: Places each rectangle in the position that minimizes the increase to overall strip height, processing items in insertion order
  • Real-time Processing: Handles rectangles sequentially without pre-sorting or post-processing
  • Rotation Support: Optional variant that automatically considers both rectangle orientations for better space utilization
  • Optimized Data Structures: Uses linked lists and min-heaps for high-performance operations

Installation

Install via npm

npm install best-fit-strip-pack

Install via yarn

yarn add best-fit-strip-pack

Install via pnpm

pnpm install best-fit-strip-pack

Usage

Basic Packing (Without Rotation)

import { BestFitStripPack } from 'best-fit-strip-pack';

// Create an instance for a strip of width 100 units
const bfsp = new BestFitStripPack(100);

// Insert rectangles
console.log(bfsp.insert(30, 20)); // { x: 0, y: 0 }
console.log(bfsp.insert(20, 40)); // { x: 30, y: 0 }
console.log(bfsp.insert(60, 10)); // { x: 0, y: 40 }

// Get current packed dimensions
console.log(`Used width: ${bfsp.packedWidth}, height: ${bfsp.packedHeight}`);

// Reset for a new packing sequence
bfsp.reset();

Packing with Rotation

import { BestFitStripPackRotatable } from 'best-fit-strip-pack';

const bfsp = new BestFitStripPackRotatable(100);

// The algorithm will automatically choose the best orientation
console.log(bfsp.insert(40, 60));
// { x: 0, y: 0, rotated: true } - rectangle was rotated to 60x40

console.log(bfsp.insert(30, 20));
// { x: 60, y: 0, rotated: false } - placed in original orientation

Batch Processing

import { BestFitStripPack } from 'best-fit-strip-pack';

const rectangles = [
  { width: 30, height: 40 },
  { width: 20, height: 60 },
  { width: 50, height: 30 },
  { width: 10, height: 20 },
  { width: 60, height: 50 },
];

const bfsp = new BestFitStripPack(100);

const packedItems = [];

for (const rect of rectangles) {
  const position = bfsp.insert(rect.width, rect.height);
  packedItems.push({ ...rect, ...position });
}

console.log(`Final strip height: ${bfsp.packedHeight}`);
// Final strip height: 110

console.log('Packed items:', packedItems);
// Packed items: [
//   { width: 30, height: 40, x: 0, y: 0 },
//   { width: 20, height: 60, x: 30, y: 0 },
//   { width: 50, height: 30, x: 50, y: 0 },
//   { width: 10, height: 20, x: 50, y: 30 },
//   { width: 60, height: 50, x: 0, y: 60 }
// ]

API Reference

Core Classes

BestFitStripPack

Main class implementing the Best-Fit algorithm without rotation.

new BestFitStripPack(stripWidth: number)

Properties:

  • packedHeight: number - Current total height of the packed strip
  • packedWidth: number - Current total width of the packed strip
  • stripWidth: number - Fixed width of the strip

Methods:

  • insert(width: number, height: number): { x: number; y: number }
  • reset(): void

BestFitStripPackRotatable

Extends the basic algorithm to support rectangle rotation.

new BestFitStripPackRotatable(stripWidth: number)

Properties:

  • packedHeight: number - Current total height of the packed strip
  • packedWidth: number - Current total width of the packed strip
  • stripWidth: number - Fixed width of the strip (readonly)

Methods:

  • insert(width: number, height: number): { x: number; y: number; rotated: boolean }
  • reset(): void

Code documentation

The complete API reference of the library is available at the code documentation site.

Issues and Support

If you encounter any issues or have questions, please open an issue.

License

This project is licensed under the MIT License.