Skip to content

Commit 8720096

Browse files
committed
remove binary search for level bound
slightly improves search performance, closes #6
1 parent ff3b8b2 commit 8720096

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ Running `npm run bench` with Node v8.10.0:
102102
```
103103
1000000 rectangles
104104
105-
flatbush: 252.311ms
106-
1000 searches 10%: 624.059ms
107-
1000 searches 1%: 76.290ms
108-
1000 searches 0.01%: 8.923ms
109-
110-
rbush: 1239.848ms
111-
1000 searches 10%: 973.296ms
112-
1000 searches 1%: 180.831ms
113-
1000 searches 0.01%: 17.746ms
105+
flatbush: 252.849ms
106+
1000 searches 10%: 617.473ms
107+
1000 searches 1%: 66.968ms
108+
1000 searches 0.01%: 7.818ms
109+
110+
rbush: 1083.758ms
111+
1000 searches 10%: 920.252ms
112+
1000 searches 1%: 173.104ms
113+
1000 searches 0.01%: 19.057ms
114114
```

index.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,13 @@ export default class Flatbush {
125125
}
126126

127127
let nodeIndex = this.data.length - 5;
128+
let level = this._levelBounds.length - 1;
128129
const queue = [];
129130
const results = [];
130131

131132
while (nodeIndex !== undefined) {
132133
// find the end index of the node
133-
const end = Math.min(nodeIndex + this.nodeSize * 5, upperBound(nodeIndex, this._levelBounds));
134+
const end = Math.min(nodeIndex + this.nodeSize * 5, this._levelBounds[level]);
134135

135136
// search through child nodes
136137
for (let pos = nodeIndex; pos < end; pos += 5) {
@@ -149,31 +150,18 @@ export default class Flatbush {
149150

150151
} else {
151152
queue.push(index); // node; add it to the search queue
153+
queue.push(level - 1);
152154
}
153155
}
154156

157+
level = queue.pop();
155158
nodeIndex = queue.pop();
156159
}
157160

158161
return results;
159162
}
160163
}
161164

162-
// binary search for the first value in the array bigger than the given
163-
function upperBound(value, arr) {
164-
let i = 0;
165-
let j = arr.length - 1;
166-
while (i < j) {
167-
const m = (i + j) >> 1;
168-
if (arr[m] > value) {
169-
j = m;
170-
} else {
171-
i = m + 1;
172-
}
173-
}
174-
return arr[i];
175-
}
176-
177165
// custom quicksort that sorts bbox data alongside the hilbert values
178166
function sort(values, boxes, left, right) {
179167
if (left >= right) return;

0 commit comments

Comments
 (0)