
Security News
PEP 810 Proposes Explicit Lazy Imports for Python 3.15
An opt-in lazy import keyword aims to speed up Python startups, especially CLIs, without the ecosystem-wide risks that sank PEP 690.
@xyflow/rbush
Advanced tools
Temporary fork of rbush by Vladimir Agafonkin with fixed dependencies. High-performance 2D spatial index for rectangles (based on R*-tree with bulk loading and bulk insertion algorithms)
This is a temporary fork of rbush by Volodymyr Agafonkin with fixed dependencies. It will be removed if the original library is updated.
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.
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.
Install with NPM (npm install @xyflow/rbush
).
// as a ES module
import RBush from '@xyflow/rbush';
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);
Insert an item:
const item = {
minX: 20,
minY: 40,
maxX: 30,
maxY: 50,
foo: 'bar'
};
tree.insert(item);
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();
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-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.
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.
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 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.
For "k nearest neighbors around a point" type of queries for RBush, check out rbush-knn.
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 |
npm install # install dependencies
npm test # lint the code and run tests
npm run perf # run performance benchmarks
npm run cov # report test coverage
RBush should run on Node and all major browsers that support ES5.
FAQs
Temporary fork of rbush by Vladimir Agafonkin with fixed dependencies. High-performance 2D spatial index for rectangles (based on R*-tree with bulk loading and bulk insertion algorithms)
The npm package @xyflow/rbush receives a total of 4 weekly downloads. As such, @xyflow/rbush popularity was classified as not popular.
We found that @xyflow/rbush demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
An opt-in lazy import keyword aims to speed up Python startups, especially CLIs, without the ecosystem-wide risks that sank PEP 690.
Security News
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.