Cache parsed timestamps for quicker rendering

This commit is contained in:
Cohee 2024-03-12 20:29:07 +02:00
parent 3912c67965
commit a9ec171c50

View File

@ -602,7 +602,24 @@ export function isOdd(number) {
return number % 2 !== 0; return number % 2 !== 0;
} }
const dateCache = new Map();
/**
* Cached version of moment() to avoid re-parsing the same date strings.
* @param {any} timestamp String or number representing a date.
* @returns {*} Moment object
*/
export function timestampToMoment(timestamp) { export function timestampToMoment(timestamp) {
if (dateCache.has(timestamp)) {
return dateCache.get(timestamp);
}
const moment = parseTimestamp(timestamp);
dateCache.set(timestamp, moment);
return moment;
}
function parseTimestamp(timestamp) {
if (!timestamp) { if (!timestamp) {
return moment.invalid(); return moment.invalid();
} }