From e1cceb5c58f693b925b7dc845d0cd81db4b01efc Mon Sep 17 00:00:00 2001 From: Paul Marrapese Date: Sun, 5 Apr 2020 19:06:08 -0700 Subject: [PATCH] improved performance of addLayers by preallocating array Cherry picked from https://github.com/Leaflet/Leaflet.markercluster/pull/988 --- src/MarkerClusterGroup.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 99c56e7d..db5c3d7c 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -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]; @@ -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; },