Skip to content

Commit

Permalink
Make countForDate very fast.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Maier committed Apr 26, 2018
1 parent 6606618 commit ac7ab8b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/calendar-heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function calendarHeatmap() {
var now = moment().endOf('day').toDate();
var yearAgo = moment().startOf('day').subtract(1, 'year').toDate();
var startDate = null;
var counterMap= {};
var data = [];
var max = null;
var colorRange = ['#D8E6E7', '#218380'];
Expand All @@ -32,6 +33,15 @@ function calendarHeatmap() {
chart.data = function (value) {
if (!arguments.length) { return data; }
data = value;

counterMap= {};

data.forEach(function (element, index) {
var key= moment(element.date).format( 'YYYY-MM-DD' );
var counter= counterMap[key] || 0;
counterMap[key]= counter + element.count;
});

return chart;
};

Expand Down Expand Up @@ -244,14 +254,8 @@ function calendarHeatmap() {
}

function countForDate(d) {
var count = 0;
var match = chart.data().find(function (element, index) {
return moment(element.date).isSame(d, 'day');
});
if (match) {
count = match.count;
}
return count;
var key= moment(d).format( 'YYYY-MM-DD' );
return counterMap[key] || 0;
}

function formatWeekday(weekDay) {
Expand Down

0 comments on commit ac7ab8b

Please sign in to comment.