kdbush vs rbush vs flatbush
Spatial Indexing Libraries Comparison
1 Year
kdbushrbushflatbushSimilar Packages:
What's Spatial Indexing Libraries?

Spatial indexing libraries are designed to efficiently manage and query spatial data, which is crucial for applications that involve geographical information, collision detection, or any scenario where spatial relationships are important. These libraries provide data structures that allow for fast insertion, deletion, and querying of spatial objects, significantly improving performance over naive approaches. Each library has its own unique features and optimizations that cater to different use cases and performance requirements.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
kdbush2,981,71865734.4 kB52 years agoISC
rbush2,245,6512,57548.8 kB119 months agoMIT
flatbush75,5401,47949.7 kB10a year agoISC
Feature Comparison: kdbush vs rbush vs flatbush

Data Structure

  • kdbush:

    KDBush employs a K-D tree structure, which is particularly effective for organizing points in a multi-dimensional space. This structure allows for quick nearest-neighbor searches and range queries, making it ideal for applications that require spatial proximity calculations.

  • rbush:

    RBush utilizes a balanced R-tree structure, which is optimized for both point and rectangle data. This allows for efficient querying of spatial relationships and is particularly useful in applications that require complex spatial operations.

  • flatbush:

    Flatbush uses a compact array-based structure that minimizes memory usage while providing fast access times. It is designed to handle point data efficiently, making it suitable for applications like mapping and geolocation services.

Performance

  • kdbush:

    KDBush provides excellent performance for 2D point data, with quick query times for nearest-neighbor searches. Its lightweight nature ensures that it remains efficient even as the dataset grows, making it suitable for real-time applications.

  • rbush:

    RBush is designed for high performance with larger datasets, providing efficient querying capabilities for both point and rectangle data. Its balanced structure ensures that query times remain consistent, even as data is added or removed.

  • flatbush:

    Flatbush is optimized for speed, offering fast insertion and query times, especially for point data. Its design minimizes overhead, making it one of the fastest options for applications that prioritize performance over complex spatial relationships.

Use Cases

  • kdbush:

    KDBush is well-suited for applications that need to perform frequent spatial queries on moderate datasets, such as interactive maps, location-based services, and any application where quick access to spatial data is necessary.

  • rbush:

    RBush is versatile and can be used in a variety of applications, including GIS systems, game development for collision detection, and any scenario that involves complex spatial queries and relationships.

  • flatbush:

    Flatbush is ideal for applications that require fast spatial queries on point data, such as mapping applications, geolocation services, and real-time data visualization where speed is critical.

Ease of Use

  • kdbush:

    KDBush is designed to be simple and lightweight, making it easy to implement and use. Its minimalistic approach allows developers to quickly get started with spatial indexing without extensive setup.

  • rbush:

    RBush provides a more comprehensive API that supports a wider range of spatial operations. While it may require a bit more understanding to utilize effectively, its capabilities make it a powerful tool for complex applications.

  • flatbush:

    Flatbush offers a straightforward API that is easy to integrate into existing projects. Its simplicity makes it accessible for developers who need to implement spatial indexing without a steep learning curve.

Memory Efficiency

  • kdbush:

    KDBush strikes a good balance between performance and memory usage, making it a great choice for applications that require efficient spatial indexing without excessive memory overhead.

  • rbush:

    RBush is designed to handle larger datasets efficiently, but it may consume more memory compared to Flatbush and KDBush. It is best suited for applications where the complexity of spatial queries justifies the additional memory usage.

  • flatbush:

    Flatbush is highly memory-efficient due to its compact array-based structure, making it suitable for applications where memory usage is a concern, such as mobile applications or embedded systems.

How to Choose: kdbush vs rbush vs flatbush
  • kdbush:

    Select KDBush if you are looking for a lightweight and simple solution that provides excellent performance for 2D point data. It is especially suitable for applications that require frequent updates and queries on a moderate dataset size, as it balances speed and memory usage effectively.

  • rbush:

    Opt for RBush if you require a robust and versatile spatial index that supports both point and rectangle data. It is well-suited for applications that need to handle complex spatial queries and offers excellent performance for larger datasets.

  • flatbush:

    Choose Flatbush if you need a fast and memory-efficient spatial index that supports dynamic data and is particularly optimized for point data. It is ideal for applications that require rapid querying of spatial data with minimal overhead.

README for kdbush

KDBush

A very fast static spatial index for 2D points based on a flat KD-tree. Compared to RBush:

  • Points only — no rectangles.
  • Static — you can't add/remove items after initial indexing.
  • Faster indexing and search, with lower memory footprint.
  • Index is stored as a single array buffer (so you can transfer it between threads or store it as a compact file).

If you need a static index for rectangles, not only points, see Flatbush. When indexing points, KDBush has the advantage of taking ~2x less memory than Flatbush.

Build Status Simply Awesome

Usage

// initialize KDBush for 1000 items
const index = new KDBush(1000);

// fill it with 1000 points
for (const {x, y} of items) {
    index.add(x, y);
}

// perform the indexing
index.finish();

// make a bounding box query
const foundIds = index.range(minX, minY, maxX, maxY);

// map ids to original items
const foundItems = foundIds.map(i => items[i]);

// make a radius query
const neighborIds = index.within(x, y, 5);

// instantly transfer the index from a worker to the main thread
postMessage(index.data, [index.data]);

// reconstruct the index from a raw array buffer
const index = KDBush.from(e.data);

Install

Install with NPM: npm install kdbush, then import as a module:

import KDBush from 'kdbush';

Or use as a module directly in the browser with jsDelivr:

<script type="module">
    import KDBush from 'https://cdn.jsdelivr.net/npm/kdbush/+esm';
</script>

Alternatively, there's a browser bundle with a KDBush global variable:

<script src="https://cdn.jsdelivr.net/npm/kdbush"></script>

API

new KDBush(numItems[, nodeSize, ArrayType])

Creates an index that will hold a given number of points (numItems). Additionally accepts:

  • nodeSize: Size of the KD-tree node, 64 by default. Higher means faster indexing but slower search, and vise versa.
  • ArrayType: Array type to use for storing coordinate values. Float64Array by default, but if your coordinates are integer values, Int32Array makes the index faster and smaller.

index.add(x, y)

Adds a given point to the index. Returns a zero-based, incremental number that represents the newly added point.

index.range(minX, minY, maxX, maxY)

Finds all items within the given bounding box and returns an array of indices that refer to the order the items were added (the values returned by index.add(x, y)).

index.within(x, y, radius)

Finds all items within a given radius from the query point and returns an array of indices.

KDBush.from(data)

Recreates a KDBush index from raw ArrayBuffer data (that's exposed as index.data on a previously indexed KDBush instance). Very useful for transferring or sharing indices between threads or storing them in a file.

Properties

  • data: array buffer that holds the index.
  • numItems: number of stored items.
  • nodeSize: number of items in a KD-tree node.
  • ArrayType: array type used for internal coordinates storage.
  • IndexArrayType: array type used for internal item indices storage.