Skip to content

Commit 9610f05

Browse files
pmarrapeseJulian Dehm
authored andcommitted
improved performance of addLayers by preallocating array
Cherry picked from Leaflet#988
1 parent b2512ac commit 9610f05

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/MarkerClusterGroup.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ export var MarkerClusterGroup = L.MarkerClusterGroup = L.FeatureGroup.extend({
292292

293293
process();
294294
} else {
295-
var needsClustering = this._needsClustering;
295+
var needsClustering = new Array(l - offset); // improve performance by preallocating the maximum size of our array
296+
var tail = 0;
296297

297298
for (; offset < l; offset++) {
298299
m = layersArray[offset];
@@ -318,8 +319,11 @@ export var MarkerClusterGroup = L.MarkerClusterGroup = L.FeatureGroup.extend({
318319
continue;
319320
}
320321

321-
needsClustering.push(m);
322+
needsClustering[tail++] = m;
322323
}
324+
325+
needsClustering = needsClustering.slice(0, tail); // truncate empty elements
326+
this._needsClustering.push.apply(this._needsClustering, needsClustering);
323327
}
324328
return this;
325329
},

0 commit comments

Comments
 (0)