Skip to content
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

[performance] faster major tick calculation #6307

Merged
merged 3 commits into from Jun 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/scales/scale.time.js
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