Skip to content

Commit 23024b2

Browse files
committed
Call click-to-select's isPointOrBinSelected as late as possible [1852]
- Reason: isPointOrBinSelected is a potentially costly operation and thus leverage an assignment in an if condition trick to avoid executing isPointOrBinSelected unnecessarily.
1 parent e7c2240 commit 23024b2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/plots/cartesian/select.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,14 @@ function selectOnClick(evt, gd, xAxes, yAxes, subplot, dragOptions, polygonOutli
311311
var clickedPtInfo = extractClickedPtInfo(hoverData, searchTraces);
312312
var isBinnedTrace = clickedPtInfo.pointNumbers.length > 0;
313313

314-
// TODO perf: call potentially costly operation (see impl comment) only when needed
315-
pointOrBinSelected = isPointOrBinSelected(clickedPtInfo);
316314

317-
if(pointOrBinSelected &&
318-
(isBinnedTrace ?
315+
// Note: potentially costly operation isPointOrBinSelected is
316+
// called as late as possible through the use of an assignment
317+
// in an if condition.
318+
if((isBinnedTrace ?
319319
isOnlyThisBinSelected(searchTraces, clickedPtInfo) :
320-
isOnlyOnePointSelected(searchTraces)))
320+
isOnlyOnePointSelected(searchTraces)) &&
321+
(pointOrBinSelected = isPointOrBinSelected(clickedPtInfo)))
321322
{
322323
if(polygonOutlines) polygonOutlines.remove();
323324
for(i = 0; i < searchTraces.length; i++) {
@@ -333,7 +334,10 @@ function selectOnClick(evt, gd, xAxes, yAxes, subplot, dragOptions, polygonOutli
333334
gd.emit('plotly_deselect', null);
334335
}
335336
} else {
336-
subtract = evt.shiftKey && pointOrBinSelected;
337+
subtract = evt.shiftKey &&
338+
(pointOrBinSelected !== undefined ?
339+
pointOrBinSelected :
340+
isPointOrBinSelected(clickedPtInfo));
337341
currentPolygon = createPtNumTester(clickedPtInfo.pointNumber, clickedPtInfo.searchInfo, subtract);
338342

339343
var concatenatedPolygons = dragOptions.polygons.concat([currentPolygon]);

0 commit comments

Comments
 (0)