rbush vs rtree
Spatial Indexing Libraries Comparison
1 Year
rbushrtree
What's Spatial Indexing Libraries?

Spatial indexing libraries are designed to efficiently store and query spatial data, such as points, rectangles, and polygons. These libraries enable fast spatial searches, which are crucial for applications involving geographical data, game development, and any scenario where spatial relationships need to be analyzed. They optimize the performance of spatial queries, allowing developers to handle large datasets with ease and speed. Both rbush and rtree are popular choices for implementing spatial indexing, but they have different underlying algorithms and use cases that can influence their selection based on project requirements.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
rbush3,125,8652,53148.8 kB116 months agoMIT
rtree2,965231-7-MIT
Feature Comparison: rbush vs rtree

Performance

  • rbush:

    Rbush is designed for high performance, especially in scenarios with frequent updates. It uses a bulk-loading algorithm for efficient insertion, making it faster than traditional R-trees when dealing with large datasets. Its performance is particularly notable in point and rectangle queries, where it can achieve logarithmic time complexity.

  • rtree:

    Rtree provides good performance for static datasets but may not be as fast as rbush for dynamic datasets. Its performance can degrade with frequent insertions and deletions due to the need for rebalancing. However, it excels in handling complex spatial queries and can efficiently manage multi-dimensional data.

Data Structure

  • rbush:

    Rbush employs a variant of the R-tree data structure optimized for speed and memory efficiency. It uses a bounding box approach to store spatial objects, allowing for quick access and retrieval. This makes it particularly effective for applications requiring rapid spatial searches.

  • rtree:

    Rtree is a more traditional R-tree structure that organizes spatial data into a hierarchy of bounding boxes. It is designed to minimize overlap and maximize the use of space, which helps improve query performance. Its structure is well-suited for a variety of spatial data types, including polygons and multi-dimensional points.

Use Cases

  • rbush:

    Rbush is ideal for applications that require real-time updates and fast querying, such as mapping applications, game development, and any scenario where spatial data changes frequently. Its efficiency in handling dynamic datasets makes it a go-to choice for developers needing quick access to spatial information.

  • rtree:

    Rtree is better suited for applications with static datasets where complex spatial relationships need to be analyzed, such as geographic information systems (GIS) and spatial databases. It provides robust querying capabilities for multi-dimensional data, making it a solid choice for analytical applications.

Complexity

  • rbush:

    Rbush has a simpler API and is easier to integrate into projects, making it a great choice for developers who need a straightforward solution for spatial indexing without the overhead of complex configurations.

  • rtree:

    Rtree can be more complex to implement due to its traditional structure and additional features. It may require a deeper understanding of spatial indexing concepts, which could pose a steeper learning curve for some developers.

Community and Support

  • rbush:

    Rbush has an active community and is well-documented, providing ample resources for developers. Its performance-oriented design has garnered attention, leading to a growing number of users and contributors.

  • rtree:

    Rtree has been around longer and has a more established community, which can be beneficial for finding support and resources. Its traditional approach means there are numerous examples and documentation available, but it may not be as actively maintained as newer libraries.

How to Choose: rbush vs rtree
  • rbush:

    Choose rbush if you need a high-performance R-tree implementation optimized for point and rectangle queries. It is particularly suited for applications that require fast insertion and deletion of spatial objects, making it ideal for dynamic datasets where objects frequently change.

  • rtree:

    Choose rtree if you prefer a more traditional R-tree implementation that supports a broader range of spatial queries and is well-suited for static datasets. Rtree is often chosen for applications that require robust spatial indexing with a focus on complex spatial relationships.

README for rbush

RBush

RBush is a high-performance JavaScript library for 2D spatial indexing of points and rectangles. It's based on an optimized R-tree data structure with bulk insertion support.

Spatial index is a special data structure for points and rectangles that allows you to perform queries like "all items within this bounding box" very efficiently (e.g. hundreds of times faster than looping over all items). It's most commonly used in maps and data visualizations.

Node

Demos

The demos contain visualization of trees generated from 50k bulk-loaded random points. Open web console to see benchmarks; click on buttons to insert or remove items; click to perform search under the cursor.

Usage

Installing RBush

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

import RBush from 'rbush';

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

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

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

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

Creating a Tree

const tree = new RBush();

An optional argument to RBush defines the maximum number of entries in a tree node. 9 (used by default) is a reasonable choice for most applications. Higher value means faster insertion and slower search, and vice versa.

const tree = new RBush(16);

Adding Data

Insert an item:

const item = {
    minX: 20,
    minY: 40,
    maxX: 30,
    maxY: 50,
    foo: 'bar'
};
tree.insert(item);

Removing Data

Remove a previously inserted item:

tree.remove(item);

By default, RBush removes objects by reference. However, you can pass a custom equals function to compare by value for removal, which is useful when you only have a copy of the object you need removed (e.g. loaded from server):

tree.remove(itemCopy, (a, b) => {
    return a.id === b.id;
});

Remove all items:

tree.clear();

Data Format

By default, RBush assumes the format of data points to be an object with minX, minY, maxX and maxY properties. You can customize this by overriding toBBox, compareMinX and compareMinY methods like this:

class MyRBush extends RBush {
    toBBox([x, y]) { return {minX: x, minY: y, maxX: x, maxY: y}; }
    compareMinX(a, b) { return a.x - b.x; }
    compareMinY(a, b) { return a.y - b.y; }
}
const tree = new MyRBush();
tree.insert([20, 50]); // accepts [x, y] points

If you're indexing a static list of points (you don't need to add/remove points after indexing), you should use kdbush which performs point indexing 5-8x faster than RBush.

Bulk-Inserting Data

Bulk-insert the given data into the tree:

tree.load([item1, item2, ...]);

Bulk insertion is usually ~2-3 times faster than inserting items one by one. After bulk loading (bulk insertion into an empty tree), subsequent query performance is also ~20-30% better.

Note that when you do bulk insertion into an existing tree, it bulk-loads the given data into a separate tree and inserts the smaller tree into the larger tree. This means that bulk insertion works very well for clustered data (where items in one update are close to each other), but makes query performance worse if the data is scattered.

Search

const result = tree.search({
    minX: 40,
    minY: 20,
    maxX: 80,
    maxY: 70
});

Returns an array of data items (points or rectangles) that the given bounding box intersects.

Note that the search method accepts a bounding box in {minX, minY, maxX, maxY} format regardless of the data format.

const allItems = tree.all();

Returns all items of the tree.

Collisions

const result = tree.collides({minX: 40, minY: 20, maxX: 80, maxY: 70});

Returns true if there are any items intersecting the given bounding box, otherwise false.

Export and Import

// export data as JSON object
const treeData = tree.toJSON();

// import previously exported data
const tree = rbush(9).fromJSON(treeData);

Importing and exporting as JSON allows you to use RBush on both the server (using Node.js) and the browser combined, e.g. first indexing the data on the server and and then importing the resulting tree data on the client for searching.

Note that the nodeSize option passed to the constructor must be the same in both trees for export/import to work properly.

K-Nearest Neighbors

For "k nearest neighbors around a point" type of queries for RBush, check out rbush-knn.

Performance

The following sample performance test was done by generating random uniformly distributed rectangles of ~0.01% area and setting maxEntries to 16 (see debug/perf.js script). Performed with Node.js v6.2.2 on a Retina Macbook Pro 15 (mid-2012).

Test | RBush | old RTree | Improvement ---------------------------- | ------ | ------ | ---- insert 1M items one by one | 3.18s | 7.83s | 2.5x 1000 searches of 0.01% area | 0.03s | 0.93s | 30x 1000 searches of 1% area | 0.35s | 2.27s | 6.5x 1000 searches of 10% area | 2.18s | 9.53s | 4.4x remove 1000 items one by one | 0.02s | 1.18s | 50x bulk-insert 1M items | 1.25s | n/a | 6.7x

Algorithms Used

  • single insertion: non-recursive R-tree insertion with overlap minimizing split routine from R*-tree (split is very effective in JS, while other R*-tree modifications like reinsertion on overflow and overlap minimizing subtree search are too slow and not worth it)
  • single deletion: non-recursive R-tree deletion using depth-first tree traversal with free-at-empty strategy (entries in underflowed nodes are not reinserted, instead underflowed nodes are kept in the tree and deleted only when empty, which is a good compromise of query vs removal performance)
  • bulk loading: OMT algorithm (Overlap Minimizing Top-down Bulk Loading) combined with Floyd–Rivest selection algorithm
  • bulk insertion: STLT algorithm (Small-Tree-Large-Tree)
  • search: standard non-recursive R-tree search

Papers

Development

npm ci       # install dependencies
npm test     # lint the code and run tests
npm run perf # run performance benchmarks
npm run cov  # report test coverage

Compatibility

RBush v4+ is published as a ES module and no longer supports CommonJS environments. It works universally in modern browsers, but you can transpile the code on your end to support IE11.