Skip to content

Commit

Permalink
improved performance of addLayers by preallocating array
Browse files Browse the repository at this point in the history
Cherry picked from Leaflet#988
  • Loading branch information
pmarrapese authored and rmader committed Apr 28, 2020
1 parent 2e821d8 commit e1cceb5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/MarkerClusterGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ export var MarkerClusterGroup = L.MarkerClusterGroup = L.FeatureGroup.extend({

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

for (; offset < l; offset++) {
m = layersArray[offset];
Expand All @@ -317,8 +318,11 @@ export var MarkerClusterGroup = L.MarkerClusterGroup = L.FeatureGroup.extend({
continue;
}

needsClustering.push(m);
needsClustering[tail++] = m;
}

needsClustering = needsClustering.slice(0, tail); // truncate empty elements
this._needsClustering.push.apply(this._needsClustering, needsClustering);
}
return this;
},
Expand Down

0 comments on commit e1cceb5

Please sign in to comment.