Commit a57bc066 authored by Sean Hammond's avatar Sean Hammond

Cache date formatters using Intl.DateTimeFormat

toLocaleDateString() creates a new formatter on each call which is
expensive.
parent 68e9b81c
...@@ -42,13 +42,31 @@ function nHr(date, now) { ...@@ -42,13 +42,31 @@ function nHr(date, now) {
return '{} hr'.replace('{}', Math.floor(delta(date, now) / hour)); return '{} hr'.replace('{}', Math.floor(delta(date, now) / hour));
} }
// Cached DateTimeFormat instance, instantiating a DateTimeFormat is expensive.
var dayAndMonthFormatter;
function dayAndMonth(date) { function dayAndMonth(date) {
return date.toLocaleDateString(undefined, {day: '2-digit', month: 'short'}); if (!dayAndMonthFormatter) {
dayAndMonthFormatter = new Intl.DateTimeFormat(undefined, {
month: 'short',
day: '2-digit',
});
}
return dayAndMonthFormatter.format(date);
} }
// Cached DateTimeFormat instance, instantiating a DateTimeFormat is expensive.
var dayMonthAndYearFormatter;
function dayAndMonthAndYear(date) { function dayAndMonthAndYear(date) {
return date.toLocaleDateString( if (!dayMonthAndYearFormatter) {
undefined, {day: '2-digit', month: 'short', year: 'numeric'}); dayMonthAndYearFormatter = new Intl.DateTimeFormat(undefined, {
day: '2-digit',
month: 'short',
year: 'numeric'
});
}
return dayMonthAndYearFormatter.format(date);
} }
var BREAKPOINTS = [ var BREAKPOINTS = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment