Skip to content

Commit 630816f

Browse files
authored
Merge pull request #1544 from hy9be/perf-setcategoryindex
Optimize performance of setCategoryIndex
2 parents 6e547ef + 14d6649 commit 630816f

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/plots/cartesian/set_convert.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,33 @@ 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+
if(ax._categoriesMap === undefined) {
130+
ax._categoriesMap = {};
131+
}
132+
133+
if(ax._categoriesMap[v] !== undefined) {
134+
return ax._categoriesMap[v];
135+
} else {
131136
ax._categories.push(v);
132-
return ax._categories.length - 1;
137+
138+
var curLength = ax._categories.length - 1;
139+
ax._categoriesMap[v] = curLength;
140+
141+
return curLength;
133142
}
134-
return c;
135143
}
136144
return BADNUM;
137145
}
138146

139147
function getCategoryIndex(v) {
140148
// d2l/d2c variant that that won't add categories but will also
141149
// allow numbers to be mapped to the linearized axis positions
142-
var index = ax._categories.indexOf(v);
143-
if(index !== -1) return index;
144-
if(typeof v === 'number') return v;
150+
if(ax._categoriesMap) {
151+
var index = ax._categoriesMap[v];
152+
if(index !== undefined) return index;
153+
}
154+
155+
if(typeof v === 'number') { return v; }
145156
}
146157

147158
function l2p(v) {
@@ -325,6 +336,8 @@ module.exports = function setConvert(ax, fullLayout) {
325336

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

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

src/plots/plots.js

+9
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,13 @@ plots.doCalcdata = function(gd, traces) {
19551955
// to be filled in later by ax.d2c
19561956
for(i = 0; i < axList.length; i++) {
19571957
axList[i]._categories = axList[i]._initialCategories.slice();
1958+
1959+
// Build the lookup map for initialized categories
1960+
axList[i]._categoriesMap = {};
1961+
for(j = 0; j < axList[i]._categories.length; j++) {
1962+
axList[i]._categoriesMap[axList[i]._categories[j]] = j;
1963+
}
1964+
19581965
if(axList[i].type === 'category') hasCategoryAxis = true;
19591966
}
19601967

@@ -1999,6 +2006,8 @@ plots.doCalcdata = function(gd, traces) {
19992006
axList[i]._min = [];
20002007
axList[i]._max = [];
20012008
axList[i]._categories = [];
2009+
// Reset the look up map
2010+
axList[i]._categoriesMap = {};
20022011
}
20032012
}
20042013

test/jasmine/tests/axes_test.js

+2
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,7 @@ describe('Test axes', function() {
18541854
var ax = {
18551855
type: 'category',
18561856
_categories: ['a', 'b', 'c', 'd'],
1857+
_categoriesMap: {'a': 0, 'b': 1, 'c': 2, 'd': 3},
18571858
tickmode: 'array',
18581859
tickvals: ['a', 1, 1.5, 'c', 2.7, 3, 'e', 4, 5, -2],
18591860
ticktext: ['A!', 'B?', 'B->C'],
@@ -1973,6 +1974,7 @@ describe('Test axes', function() {
19731974

19741975
function _autoBin(x, ax, nbins) {
19751976
ax._categories = [];
1977+
ax._categoriesMap = {};
19761978
Axes.setConvert(ax);
19771979

19781980
var d = ax.makeCalcdata({ x: x }, 'x');

0 commit comments

Comments
 (0)