Skip to content

Commit 3858dfd

Browse files
committed
Improve autoSkip performance
1 parent e6cf2fe commit 3858dfd

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/core/core.scale.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,6 @@ function parseTickFontOptions(options) {
212212
return {minor: minor, major: major};
213213
}
214214

215-
function nonSkipped(ticksToFilter) {
216-
var filtered = [];
217-
var item, index, len;
218-
for (index = 0, len = ticksToFilter.length; index < len; ++index) {
219-
item = ticksToFilter[index];
220-
if (typeof item._index !== 'undefined') {
221-
filtered.push(item);
222-
}
223-
}
224-
return filtered;
225-
}
226-
227215
function getEvenSpacing(arr) {
228216
var len = arr.length;
229217
var i, diff;
@@ -272,7 +260,7 @@ function getMajorIndices(ticks) {
272260
return result;
273261
}
274262

275-
function skipMajors(ticks, majorIndices, spacing) {
263+
function skipMajors(ticks, newTicks, majorIndices, spacing) {
276264
var count = 0;
277265
var next = majorIndices[0];
278266
var i, tick;
@@ -281,16 +269,14 @@ function skipMajors(ticks, majorIndices, spacing) {
281269
for (i = 0; i < ticks.length; i++) {
282270
tick = ticks[i];
283271
if (i === next) {
284-
tick._index = i;
272+
newTicks.push(tick);
285273
count++;
286274
next = majorIndices[count * spacing];
287-
} else {
288-
delete tick.label;
289275
}
290276
}
291277
}
292278

293-
function skip(ticks, spacing, majorStart, majorEnd) {
279+
function skip(ticks, newTicks, spacing, majorStart, majorEnd) {
294280
var start = valueOrDefault(majorStart, 0);
295281
var end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length);
296282
var count = 0;
@@ -312,11 +298,9 @@ function skip(ticks, spacing, majorStart, majorEnd) {
312298
for (i = Math.max(start, 0); i < end; i++) {
313299
tick = ticks[i];
314300
if (i === next) {
315-
tick._index = i;
301+
newTicks.push(tick);
316302
count++;
317303
next = Math.round(start + count * spacing);
318-
} else {
319-
delete tick.label;
320304
}
321305
}
322306
}
@@ -918,27 +902,28 @@ class Scale extends Element {
918902
var numMajorIndices = majorIndices.length;
919903
var first = majorIndices[0];
920904
var last = majorIndices[numMajorIndices - 1];
905+
var newTicks = [];
921906
var i, ilen, spacing, avgMajorSpacing;
922907

923908
// If there are too many major ticks to display them all
924909
if (numMajorIndices > ticksLimit) {
925-
skipMajors(ticks, majorIndices, numMajorIndices / ticksLimit);
926-
return nonSkipped(ticks);
910+
skipMajors(ticks, newTicks, majorIndices, numMajorIndices / ticksLimit);
911+
return newTicks;
927912
}
928913

929914
spacing = calculateSpacing(majorIndices, ticks, axisLength, ticksLimit);
930915

931916
if (numMajorIndices > 0) {
932917
for (i = 0, ilen = numMajorIndices - 1; i < ilen; i++) {
933-
skip(ticks, spacing, majorIndices[i], majorIndices[i + 1]);
918+
skip(ticks, newTicks, spacing, majorIndices[i], majorIndices[i + 1]);
934919
}
935920
avgMajorSpacing = numMajorIndices > 1 ? (last - first) / (numMajorIndices - 1) : null;
936-
skip(ticks, spacing, helpers.isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first);
937-
skip(ticks, spacing, last, helpers.isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing);
938-
return nonSkipped(ticks);
921+
skip(ticks, newTicks, spacing, helpers.isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first);
922+
skip(ticks, newTicks, spacing, last, helpers.isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing);
923+
return newTicks;
939924
}
940-
skip(ticks, spacing);
941-
return nonSkipped(ticks);
925+
skip(ticks, newTicks, spacing);
926+
return newTicks;
942927
}
943928

944929
/**

0 commit comments

Comments
 (0)