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

@zeitfall/quadtree

v1.0.1

Published

Implementation of point-region quadtree algorithm

Downloads

253

Readme

Overview

A TypeScript implementation of a region-based QuadTree for spatial partitioning of 2D point data, supporting generic payloads, capacity-based subdivision, and depth-limiting.

Mathematical & Algorithmic Properties

| Operation / Metric | Average Case | Worst Case | | --- | --- | --- | | Insertion | O(log n) | O(n) | | Spatial Query | O(log n + k) | O(n) | | Space Complexity | O(n) | O(n) |

Note: n represents the total number of inserted points; k represents the number of points intersecting the query region.

API Reference

QuadTree<I unknown>

The primary structural class representing a node within the spatial tree.

  • Constructor constructor(boundary: QuadTreeBoundary, threshold: number, depth = 0, maxDepth = 8) Initializes a node with a defined spatial boundary, capacity threshold, current depth layer, and structural depth limit.

  • insert(x: number, y: number, value: I): boolean Inserts a point payload into the tree. Returns true if insertion succeeds, or false if the coordinates fall outside the node boundary.

  • Mechanism: Governed by internal fields #threshold, #depth, and #maxDepth. If the node capacity exceeds #threshold and #depth is less than #maxDepth, #subdivide() is executed, transforming the leaf into an internal node and redistributing existing payloads to four child quadrants (#nodeNW, #nodeNE, #nodeSW, #nodeSE).

  • query(region: QuadTreeRegion, result: QuadTreeInsertable<I>[]): void Populates the result array with all payloads contained within the specified QuadTreeRegion. Evaluates intersection via region.intersects before performing point-in-boundary checks.

  • clear(): void Clears all internal point references and recursively clears child node instances.

QuadTreeBoundary

Implements QuadTreeRegion. Defines the spatial bounding box for a node.

  • Constructor constructor(x: number, y: number, width: number, height: number) Sets the origin coordinates (x, y) and spatial dimensions (width, height).
  • Properties x: number, y: number, width: number, height: number, left: number, right: number, top: number, bottom: number
  • contains(x: number, y: number): boolean Evaluates whether a point falls within the spatial boundaries.
  • intersects(other: QuadTreeBoundary): boolean Evaluates Axis-Aligned Bounding Box (AABB) intersection between two boundaries.

QuadTreeRegion

An interface defining spatial bounds evaluation.

  • contains(x: number, y: number): boolean
  • intersects(boundary: QuadTreeBoundary): boolean

QuadTreeInsertable<I unknown>

An interface representing the structured storage layout of data elements.

  • Properties x: number, y: number, value: I

Operational Context (Behavioral Notes)

  • Boundary Conditions: Coordinate boundaries evaluate containment using inclusive lower bounds and exclusive upper bounds: [x, x + width) and [y, y + height).
  • Subdivision Behavior: Structural subdivision is lazy. Node splitting occurs only when point allocations exceed the target node #threshold, provided the maximum structural depth limitation (#maxDepth) has not been reached.
  • Clearing Mechanics: The .clear() routine executes a post-order traversal to empty local point arrays, unset structural split states, and nullify all internal child node pointers, freeing references for immediate garbage collection.

Usage Example

import { QuadTree, QuadTreeBoundary, QuadTreeInsertable } from './quadtree';

const boundary = new QuadTreeBoundary(0, 0, 200, 200);
const quadTree = new QuadTree<string>(boundary, 2, 0, 4);

quadTree.insert(10, 15, "Payload_A");
quadTree.insert(45, 80, "Payload_B");
quadTree.insert(12, 18, "Payload_C"); 

const queryRegion = new QuadTreeBoundary(0, 0, 50, 50);
const matchedPoints: QuadTreeInsertable<string>[] = [];

quadTree.query(queryRegion, matchedPoints);