Skip to content

Commit

Permalink
Optimize filterBetween
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Nov 3, 2019
1 parent d12b00b commit b926551
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,28 @@ function getTimestampsForTicks(scale) {
return timestamps;
}

/**
* Return subset of `timestamps` between `min` and `max`.
* Timestamps are assumend to be in sorted order.
* @param {int[]} timestamps - array of timestaMps
* @param {int} min - min value (timestamp)
* @param {int} max - max value (timestamp)
*/
function filterBetween(timestamps, min, max) {
var ticks = [];
var i, ilen, timestamp;
var start = 0;
var end = timestamps.length - 1;

// Remove ticks outside the min/max range
for (i = 0, ilen = timestamps.length; i < ilen; ++i) {
timestamp = timestamps[i];
if (timestamp >= min && timestamp <= max) {
ticks.push(timestamp);
}
while (start < end && timestamps[start] < min) {
start++;
}
return ticks;
while (end > start && timestamps[end] > max) {
end--;
}
end++; // slice does not include last element

return start > 0 || end < timestamps.length
? timestamps.slice(start, end)
: timestamps;
}

var defaultConfig = {
Expand Down

0 comments on commit b926551

Please sign in to comment.