Skip to content

Commit

Permalink
Merge pull request Leaflet#988 from pmarrapese/addLayers-perf-fix
Browse files Browse the repository at this point in the history
Improve performance of addLayers by preallocating array
  • Loading branch information
KristjanESPERANTO authored Dec 22, 2024
2 parents c0f055b + f43d89c commit e9a8341
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 @@ -292,7 +292,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 @@ -318,8 +319,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 = this._needsClustering.concat(needsClustering);
}
return this;
},
Expand Down

0 comments on commit e9a8341

Please sign in to comment.