Skip to content

Commit efe5029

Browse files
authored
Merge pull request #1355 from OpenGeoscience/point-search-sort-speed
perf: Speed up hit tests for points and markers
2 parents 0bf0d76 + 901921f commit efe5029

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/markerFeature.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ var markerFeature = function (arg) {
228228
// Find markers inside the bounding box
229229
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
230230

231-
idx.sort((a, b) => a - b);
231+
idx = Uint32Array.from(idx).sort();
232232
// Filter by circular region
233233
idx.forEach(function (i) {
234234
var d = data[i],
@@ -353,16 +353,20 @@ var markerFeature = function (arg) {
353353
};
354354
// Find markers inside the bounding box. Only these could be in the polygon
355355
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
356-
// sort by index
357-
idx.sort((a, b) => a - b);
356+
/* sort by index. This had been
357+
* idx.sort((a, b) => a - b);
358+
* but this requires continual casting from int to str and back, so using
359+
* a Uint32Array is faster, though potentially limits the maximum number of
360+
* markers. */
361+
idx = Uint32Array.from(idx).sort();
358362
// filter markers within the polygon
359363
idx.forEach(function (i) {
360364
var d = data[i];
361365
let p = m_this.position()(d, i);
362-
let rad = radius(data[i], i),
363-
swz = scaleWithZoom(data[i], i);
364-
const so = strokeOffset(data[i], i),
365-
s = swz ? strokeWidth(data[i], i) : 0;
366+
let rad = radius(d, i),
367+
swz = scaleWithZoom(d, i);
368+
const so = strokeOffset(d, i),
369+
s = swz ? strokeWidth(d, i) : 0;
366370
let ris = radiusIncludesStroke(d, i);
367371
ris = ris === undefined ? true : ris;
368372
const rwos = ris ? rad + s * (so - 1) / 2 : rad; // radius without stroke

src/pointFeature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ var pointFeature = function (arg) {
329329
// Find points inside the bounding box
330330
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
331331

332-
idx.sort((a, b) => a - b);
332+
idx = Uint32Array.from(idx).sort();
333333
// Filter by circular region
334334
idx.forEach(function (i) {
335335
var d = data[i],
@@ -434,7 +434,7 @@ var pointFeature = function (arg) {
434434
// Find points inside the bounding box. Only these could be in the polygon
435435
idx = m_rangeTree.range(min.x, min.y, max.x, max.y);
436436
// sort by index
437-
idx.sort((a, b) => a - b);
437+
idx = Uint32Array.from(idx).sort();
438438
// filter points within the polygon
439439
idx.forEach(function (i) {
440440
var d = data[i],

0 commit comments

Comments
 (0)