Skip to content

Commit f8d18de

Browse files
authored
Merge pull request #7301 from giuseppe-straziota/performance-improvement
fix: Performance improvement for scattergl with many points. Issue #7065
2 parents bbc30fa + 4c35d1c commit f8d18de

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

draftlogs/7301_fix.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Performance improvement for scattergl traces with many points [[#7301](https://github.com/plotly/plotly.js/pull/7301)],
2+
with thanks to @giuseppe-straziota for the contribution!

src/components/fx/helpers.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ exports.getClosest = function(cd, distfn, pointData) {
6060
// this is the longest loop... if this bogs down, we may need
6161
// to create pre-sorted data (by x or y), not sure how to
6262
// do this for 'closest'
63-
for(var i = 0; i < cd.length; i++) {
64-
var newDistance = distfn(cd[i]);
63+
64+
// defined outside the for to improve the garbage collector performance
65+
var newDistance = Infinity;
66+
// the browser engine typically optimizes the length, but it is outside the cycle if it does not
67+
var len = cd.length
68+
for(var i = 0; i < len; i++) {
69+
newDistance = distfn(cd[i]);
6570
if(newDistance <= pointData.distance) {
6671
pointData.index = i;
6772
pointData.distance = newDistance;

0 commit comments

Comments
 (0)