8000 Optimize performance of setCategoryIndex by hy9be · Pull Request #1544 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Optimize performance of setCategoryIndex #1544

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 13 commits into from
Apr 10, 2017
Merged
Next Next commit
Optimize performance of setCategoryIndex
I added an auxiliary map for ``ax._categories`` array to avoid using ``Array.prototype.indexOf`` function searching for the existence of a category.

I tested [here](https://jsfiddle.net/smileyhaowen/cvLzxz7L/2/) and observed ~20% performance improvements on big amount of traces.
  • Loading branch information
hy9be authored Mar 31, 2017
commit 40e69d39ebd4ee0f28918d58c738c9f19eaf4a2c
14 changes: 10 additions & 4 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,16 @@ module.exports = function setConvert(ax, fullLayout) {
*/
function setCategoryIndex(v) {
if(v !== null && v !== undefined) {
var c = ax._categories.indexOf(v);
if(c === -1) {

if(ax._categoriesMap[v] === undefined) {
ax._categories.push(v);
return ax._categories.length - 1;

var curLength = ax._categories.length - 1;
ax._categoriesMap[v] = curLength;

return curLength;
}
return c;
return ax._categoriesMap[v];
}
return BADNUM;
}
Expand Down Expand Up @@ -325,6 +329,8 @@ module.exports = function setConvert(ax, fullLayout) {

// TODO cleaner way to handle this case
if(!ax._categories) ax._categories = [];
// Add a map to optimize the performance of category collection
if(!ax._categoriesMap) ax._categoriesMap = {};

// make sure we have a domain (pull it in from the axis
// this one is overlaying if necessary)
Expand Down
0