diff --git a/draftlogs/7301_fix.md b/draftlogs/7301_fix.md new file mode 100644 index 00000000000..a98abe40f66 --- /dev/null +++ b/draftlogs/7301_fix.md @@ -0,0 +1,2 @@ +- Performance improvement for scattergl traces with many points [[#7301](https://github.com/plotly/plotly.js/pull/7301)], + with thanks to @giuseppe-straziota for the contribution! \ No newline at end of file diff --git a/src/components/fx/helpers.js b/src/components/fx/helpers.js index f11e5266ef7..8ba0d2fa254 100644 --- a/src/components/fx/helpers.js +++ b/src/components/fx/helpers.js @@ -60,8 +60,13 @@ exports.getClosest = function(cd, distfn, pointData) { // this is the longest loop... if this bogs down, we may need // to create pre-sorted data (by x or y), not sure how to // do this for 'closest' - for(var i = 0; i < cd.length; i++) { - var newDistance = distfn(cd[i]); + + // defined outside the for to improve the garbage collector performance + var newDistance = Infinity; + // the browser engine typically optimizes the length, but it is outside the cycle if it does not + var len = cd.length + for(var i = 0; i < len; i++) { + newDistance = distfn(cd[i]); if(newDistance <= pointData.distance) { pointData.index = i; pointData.distance = newDistance;