From 29f5a35a1fbf3d8ab6c7ceef956c71ae5e0774cc Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 3 Oct 2017 00:02:58 -0400 Subject: [PATCH] using Map for grouped changes --- static/js/recent_changes.js | 44 +++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/static/js/recent_changes.js b/static/js/recent_changes.js index 9d3e78bdb..37c42e8a7 100644 --- a/static/js/recent_changes.js +++ b/static/js/recent_changes.js @@ -9,7 +9,8 @@ $(document).bind('keydown', 'alt+r', function() { url: baseApiUrl + 'recent-changes/', type: 'GET', success: function (result) { - const groupedByDate = {}; + const groupedByDate = new Map(); + const dayCache = {}; for (const row of result) { if (row.encryption > 0) { @@ -21,43 +22,44 @@ $(document).bind('keydown', 'alt+r', function() { } } - const dateDay = getDateFromTS(row.date_modified); + + let dateDay = getDateFromTS(row.date_modified); dateDay.setHours(0); dateDay.setMinutes(0); dateDay.setSeconds(0); dateDay.setMilliseconds(0); - // FIXME: we can use Map object instead to avoid this hack - const dateDayTS = dateDay.getTime(); // we can't use dateDay as key because complex objects can't be keys - - if (!groupedByDate[dateDayTS]) { - groupedByDate[dateDayTS] = []; + // this stupidity is to make sure that we always use the same day object because Map uses only + // reference equality + if (dayCache[dateDay]) { + dateDay = dayCache[dateDay]; + } + else { + dayCache[dateDay] = dateDay; } - groupedByDate[dateDayTS].push(row); + if (!groupedByDate.has(dateDay)) { + groupedByDate.set(dateDay, []); + } + + groupedByDate.get(dateDay).push(row); } - const sortedDates = Object.keys(groupedByDate); - sortedDates.sort(); - sortedDates.reverse(); - - for (const dateDayTS of sortedDates) { + for (const [dateDay, dayChanges] of groupedByDate) { const changesListEl = $('