Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize the library for v3.0 #93

Merged
merged 4 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- "8"
- "10"
- "stable"
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ Install with NPM (`npm install rbush`), or use CDN links for browsers:
### Creating a Tree

```js
var tree = rbush();
const tree = new RBush();
```

An optional argument to `rbush` defines the maximum number of entries in a tree node.
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.

```js
var tree = rbush(16);
const tree = new RBush(16);
```

### Adding Data

Insert an item:

```js
var item = {
const item = {
minX: 20,
minY: 40,
maxX: 30,
Expand All @@ -72,7 +72,7 @@ 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):

```js
tree.remove(itemCopy, function (a, b) {
tree.remove(itemCopy, (a, b) => {
return a.id === b.id;
});
```
Expand All @@ -87,12 +87,16 @@ 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 providing an array with corresponding accessor strings
as a second argument to `rbush` like this:
You can customize this by overriding `toBBox`, `compareMinX` and `compareMinY` methods like this:

```js
var tree = rbush(9, ['[0]', '[1]', '[0]', '[1]']); // accept [x, y] points
tree.insert([20, 50]);
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](https://github.com/mourner/kdbush) which performs point indexing 5-8x faster than RBush.
Expand All @@ -119,7 +123,7 @@ but makes query performance worse if the data is scattered.
### Search

```js
var result = tree.search({
const result = tree.search({
minX: 40,
minY: 20,
maxX: 80,
Expand All @@ -130,18 +134,18 @@ var result = tree.search({
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 format specified in the constructor (which only affects inserted objects).
regardless of the data format.

```js
var allItems = tree.all();
const allItems = tree.all();
```

Returns all items of the tree.

### Collisions

```js
var result = tree.collides({minX: 40, minY: 20, maxX: 80, maxY: 70});
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`.
Expand All @@ -151,10 +155,10 @@ Returns `true` if there are any items intersecting the given bounding box, other

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

// import previously exported data
var tree = rbush(9).fromJSON(treeData);
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,
Expand Down Expand Up @@ -204,14 +208,14 @@ bulk-insert 1M items | 1.25s | n/a | 6.7x
```bash
npm install # install dependencies

npm test # check the code with JSHint and run tests
npm test # lint the code and run tests
npm run perf # run performance benchmarks
npm run cov # report test coverage (with more detailed report in coverage/lcov-report/index.html)
npm run cov # report test coverage
```

## Compatibility

RBush should run on Node and all major browsers. The only caveat: IE 8 needs an [Array#indexOf polyfill](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill) for `remove` method to work.
RBush should run on Node and all major browsers that support ES5.

## Changelog

Expand Down
10 changes: 5 additions & 5 deletions bench/bulk.bench.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var Benchmark = require('benchmark'),
rbush = require('../rbush'),
genData = require('./gendata');
import Benchmark from 'benchmark';
import RBush from '../index.js';
import {generate} from './gendata';

var N = 10000,
maxFill = 16;

var data = genData(N, 1);
var data = generate(N, 1);

new Benchmark.Suite()
.add('bulk loading ' + N + ' items (' + maxFill + ' node size)', function () {
var tree = rbush(maxFill);
var tree = new RBush(maxFill);
tree.load(data);
})
.on('error', function(event) {
Expand Down
22 changes: 11 additions & 11 deletions bench/bulksearch.bench.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
var Benchmark = require('benchmark'),
rbush = require('../rbush'),
genData = require('./gendata');
import Benchmark from 'benchmark';
import RBush from '../index.js';
import {generate} from './gendata';

var N = 10000,
maxFill = 16;

var data = genData(N, 1);
var bboxes100 = genData(1000, 100 * Math.sqrt(0.1));
var bboxes10 = genData(1000, 10);
var bboxes1 = genData(1000, 1);
var data = generate(N, 100);
var bboxes100 = generate(1000, 100 * Math.sqrt(0.1));
var bboxes10 = generate(1000, 10);
var bboxes1 = generate(1000, 1);

var tree = rbush(maxFill);
var tree = new RBush(maxFill);
tree.load(data);

new Benchmark.Suite()
.add('1000 searches 10% after bulk loading ' + N, function () {
for (i = 0; i < 1000; i++) {
for (var i = 0; i < 1000; i++) {
tree.search(bboxes100[i]);
}
})
.add('1000 searches 1% after bulk loading ' + N, function () {
for (i = 0; i < 1000; i++) {
for (var i = 0; i < 1000; i++) {
tree.search(bboxes10[i]);
}
})
.add('1000 searches 0.01% after bulk loading ' + N, function () {
for (i = 0; i < 1000; i++) {
for (var i = 0; i < 1000; i++) {
tree.search(bboxes1[i]);
}
})
Expand Down
6 changes: 2 additions & 4 deletions bench/gendata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

module.exports = genData;

function randBox(size) {
var x = Math.random() * (100 - size),
y = Math.random() * (100 - size);
Expand All @@ -9,15 +7,15 @@ function randBox(size) {
y + size * Math.random()];
}

function genData(N, size) {
export function generate(N, size) {
var data = [];
for (var i = 0; i < N; i++) {
data.push(randBox(size));
}
return data;
};

genData.convert = function (data) {
export function convert(data) {
var result = [];
for (var i = 0; i < data.length; i++) {
result.push({x: data[i][0], y: data[i][1], w: data[i][2] - data[i][0], h: data[i][3] - data[i][1]});
Expand Down
22 changes: 6 additions & 16 deletions bench/insert.bench.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
var Benchmark = require('benchmark'),
rbush = require('../rbush'),
genData = require('./gendata');
import Benchmark from 'benchmark';
import RBush from '../index.js';
import {generate} from './gendata';

var RTree = require('rtree');


var N = 10000,
var N = 100,
maxFill = 16;

var data = genData(N, 1);
var data2 = genData.convert(data);
var data = generate(N, 1);

new Benchmark.Suite()
.add('insert ' + N + ' items (' + maxFill + ' node size)', function () {
var tree = rbush(maxFill);
var tree = new RBush(maxFill);
for (var i = 0; i < N; i++) {
tree.insert(data[i]);
}
})
.add('insert ' + N + ' items (' + maxFill + ' node size), old RTree', function () {
var tree2 = new RTree(maxFill);
for (var i = 0; i < N; i++) {
tree2.insert(data2[i], i);
}
})
.on('error', function(event) {
console.log(event.target.error);
})
Expand Down
4 changes: 2 additions & 2 deletions bench/perf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import rbush from '..';
import RBush from '../index.js';

var N = 1000000,
maxFill = 16;
Expand Down Expand Up @@ -31,7 +31,7 @@ var bboxes100 = genData(1000, 100 * Math.sqrt(0.1));
var bboxes10 = genData(1000, 10);
var bboxes1 = genData(1000, 1);

var tree = rbush(maxFill);
var tree = new RBush(maxFill);

console.time('insert one by one');
for (var i = 0; i < N; i++) {
Expand Down
16 changes: 8 additions & 8 deletions bench/search.bench.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var Benchmark = require('benchmark'),
rbush = require('../rbush'),
genData = require('./gendata');
import Benchmark from 'benchmark';
import RBush from '../index.js';
import {generate} from './gendata';

var N = 10000,
maxFill = 16;

var data = genData(N, 1);
var bboxes100 = genData(1000, 100 * Math.sqrt(0.1));
var bboxes10 = genData(1000, 10);
var bboxes1 = genData(1000, 1);
var data = generate(N, 1);
var bboxes100 = generate(1000, 100 * Math.sqrt(0.1));
var bboxes10 = generate(1000, 10);
var bboxes1 = generate(1000, 1);

var tree = rbush(maxFill);
var tree = new RBush(maxFill);
for (var i = 0; i < N; i++) {
tree.insert(data[i]);
}
Expand Down
Loading