Skip to content

Optimize the performance of scatter trace sorting comparator with map #1555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/traces/scatter/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems entirely equivalent to me 👍

I didn't anticipate this would be more than like 10-15 or so, which would make the overhead of indexOf negligible. I'll keep this in mind when contemplating orders of magnitude!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, actually there is almost no difference on map and array here if the amount of trace is small. My cases might be a bit crazy (with 5000+ traces). But it seems worthwhile to use map instead here since there is almost no side effect.

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;
});

Expand Down