From b31980edab1f0b9e0a446af10c2a1d4d226566f2 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Wed, 19 Jun 2019 04:08:40 -0700 Subject: [PATCH] [performance] faster major tick calculation (#6307) * [performance] faster major tick calculation * Address review comments * Replace isNaN --- src/scales/scale.time.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 1be680ef5ed..10dd50e4f02 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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 = {