Skip to content

Commit

Permalink
[performance] faster major tick calculation (#6307)
Browse files Browse the repository at this point in the history
* [performance] faster major tick calculation

* Address review comments

* Replace isNaN
  • Loading branch information
benmccann authored and etimberg committed Jun 19, 2019
1 parent 9eecdf4 commit b31980e
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,21 +404,40 @@ function computeOffsets(table, ticks, min, max, options) {
return {start: start, end: end};
}

function setMajorTicks(scale, ticks, map, majorUnit) {
var adapter = scale._adapter;
var first = +adapter.startOf(ticks[0].value, majorUnit);
var last = ticks[ticks.length - 1].value;
var major, index;

for (major = first; major <= last; major = +adapter.add(major, 1, majorUnit)) {
index = map[major];
if (index >= 0) {
ticks[index].major = true;
}
}
return ticks;
}

function ticksFromTimestamps(scale, values, majorUnit) {
var ticks = [];
var i, ilen, value, major;
var map = {};
var ilen = values.length;
var i, value;

for (i = 0, ilen = values.length; i < ilen; ++i) {
for (i = 0; i < ilen; ++i) {
value = values[i];
major = majorUnit ? value === +scale._adapter.startOf(value, majorUnit) : false;
map[value] = i;

ticks.push({
value: value,
major: major
major: false
});
}

return ticks;
// We set the major ticks separately from the above loop because calling startOf for every tick
// is expensive when there is a large number of ticks
return (ilen === 0 || !majorUnit) ? ticks : setMajorTicks(scale, ticks, map, majorUnit);
}

var defaultConfig = {
Expand Down

0 comments on commit b31980e

Please sign in to comment.