Skip to content

Commit 40e69d3

Browse files
authored
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.
1 parent 7f63e29 commit 40e69d3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/plots/cartesian/set_convert.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,16 @@ module.exports = function setConvert(ax, fullLayout) {
126126
*/
127127
function setCategoryIndex(v) {
128128
if(v !== null && v !== undefined) {
129-
var c = ax._categories.indexOf(v);
130-
if(c === -1) {
129+
130+
if(ax._categoriesMap[v] === undefined) {
131131
ax._categories.push(v);
132-
return ax._categories.length - 1;
132+
133+
var curLength = ax._categories.length - 1;
134+
ax._categoriesMap[v] = curLength;
135+
136+
return curLength;
133137
}
134-
return c;
138+
return ax._categoriesMap[v];
135139
}
136140
return BADNUM;
137141
}
@@ -325,6 +329,8 @@ module.exports = function setConvert(ax, fullLayout) {
325329

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

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

0 commit comments

Comments
 (0)