Skip to content

Commit 07fd746

Browse files
committed
perf: Speed up cluster zoom transition
In a test of 1.3M points, this saves 500-700 ms per zoom transition in one test. The clustering took a maximum of 94ms for the zoom transition; the remaining time is in the webGL point transformation and property fetches. The next improvement would be to cache those values per zoom level.
1 parent 55abf07 commit 07fd746

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/pointFeature.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,33 @@ var pointFeature = function (arg) {
182182
return;
183183
}
184184

185+
const a = Date.now();
185186
// store the current zoom level privately
186187
m_lastZoom = z;
187188

188-
// get the raw data elements for the points at the current level
189-
var data = m_clusterTree.points(z).map(function (d) {
190-
return m_allData[d.index];
191-
});
189+
const points = m_clusterTree.points(z);
190+
const b = Date.now();
191+
const clusters = m_clusterTree.clusters(z);
192+
const c = Date.now();
193+
const data = new Array(points.length + clusters.length);
194+
for (let pidx = 0; pidx < points.length; pidx += 1) {
195+
data[pidx] = m_allData[points[pidx].index];
196+
}
192197

198+
const d = Date.now();
193199
// append the clusters at the current level
194-
m_clusterTree.clusters(z).forEach(function (d) {
200+
for (let cidx = 0, didx = points.length; cidx < clusters.length; cidx += 1, didx += 1) {
201+
const d = clusters[cidx];
195202
// mark the datum as a cluster for accessor methods
196203
d.__cluster = true;
197204

198205
// store all of the data objects for each point in the cluster as __data
199-
d.__data = [];
200-
d.obj.each(function (e) {
201-
d.__data.push(m_allData[e.index]);
202-
});
203-
data.push(d);
204-
});
206+
d.__data = new Array(d.obj.length);
207+
for (let idx = 0; idx < d.obj.length; idx += 1) {
208+
d.__data[idx] = m_allData[d.obj[idx].index];
209+
}
210+
data[didx] = d;
211+
}
205212

206213
// prevent recomputing the clustering and set the new data array
207214
m_ignoreData = true;

src/util/distanceGrid.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5555
* @copyright 2012, David Leaver
5656
*/
5757

58-
var $ = require('jquery');
5958
var L = {};
6059
L.Util = {
6160
// return unique ID of an object
@@ -181,7 +180,7 @@ DistanceGrid.prototype = {
181180

182181
/* return the point coordinates contained in the structure */
183182
contents: function () {
184-
return $.map(this._objectPoint, function (val) { return val; });
183+
return Object.values(this._objectPoint);
185184
},
186185

187186
_getCoord: function (x) {

0 commit comments

Comments
 (0)