A fast and memory-efficient 2D RTree implementation for Javascript. Supports range searches and nearest neighbour searches.


The RTree is available as:

  • script: ./release/js/RTree2d.js (adds a global variable with the name RTree2d)
  • an AMD module: ./release/js/RTree2d-amd.js
  • an CommonJS module: ./release/js/RTree2d-common.js


Take a test drive here.

At a glance

var rtree = new RTree2d();

//insert an object at the rectangle (x/y/width/height).
var ob = {store: 'me'};

//search a rectangle
var results =,0,10,10);
//results ~= [{store: "me"}];

//Instead of using search, you can also use one of the collection comprehension methods.
//(e.g. you might be doing a search 60 frames-per-second. You do not want a new array each time).
rtree.forEachinRectangle(0,0,10,10, function(ob){
    console.log("i got a hit, ",ob);

//find the k nearest neighbours (x/y/k)
var kNNArray = rtree.nearestNeighbours(0,0,1);

//Number of elements in the tree.
var size = rtree.size();

//Remove an element.


API Documentation

Code repository

Git repo on bitbucket

Dev project setup

1) checkout repo

> git clone

2) run node setup script (might need admin priviliges on windows)

> node setup.js

3) build a release

> grunt release