From 00178a213a567cd5b47a2295928fddfcaa7e1155 Mon Sep 17 00:00:00 2001 From: hy9be_uva Date: Wed, 5 Apr 2017 13:09:00 -0400 Subject: [PATCH] Optimize the scatter trace sorting comparator with map Construct a map rather than an array to improve the performance of index search. Jasmine tests passed. --- src/traces/scatter/plot.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/traces/scatter/plot.js b/src/traces/scatter/plot.js index 7da03ffe2fe..8e367807a70 100644 --- a/src/traces/scatter/plot.js +++ b/src/traces/scatter/plot.js @@ -51,13 +51,13 @@ module.exports = function plot(gd, plotinfo, cdscatter, transitionOpts, makeOnCo // Sort the traces, once created, so that the ordering is preserved even when traces // are shown and hidden. This is needed since we're not just wiping everything out // and recreating on every update. - for(i = 0, uids = []; i < cdscatter.length; i++) { - uids[i] = cdscatter[i][0].trace.uid; + for(i = 0, uids = {}; i < cdscatter.length; i++) { + uids[cdscatter[i][0].trace.uid] = i; } scatterlayer.selectAll('g.trace').sort(function(a, b) { - var idx1 = uids.indexOf(a[0].trace.uid); - var idx2 = uids.indexOf(b[0].trace.uid); + var idx1 = uids[a[0].trace.uid]; + var idx2 = uids[b[0].trace.uid]; return idx1 > idx2 ? 1 : -1; });