Skip to content

Commit

Permalink
Merge pull request #19 from moonship-fe/main
Browse files Browse the repository at this point in the history
fix: optimization getLocaleDate performances
  • Loading branch information
MaTeMaTuK committed Aug 15, 2021
2 parents 78d9e33 + 894ab6b commit fd5f4ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/components/task-list/task-list-table.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import React from "react";
import React, { useMemo } from "react";
import styles from "./task-list-table.module.css";
import { Task } from "../../types/public-types";

const localeDateStringCache = {};
const toLocaleDateStringFactory = (locale: string) => (date: Date, dateTimeOptions: Intl.DateTimeFormatOptions) => {
const key = date.toString();
let lds = localeDateStringCache[key];
if (!lds) {
lds = date.toLocaleDateString(locale, dateTimeOptions);
localeDateStringCache[key] = lds;
}
return lds;
};

export const TaskListTableDefault: React.FC<{
rowHeight: number;
rowWidth: string;
Expand All @@ -27,6 +38,8 @@ export const TaskListTableDefault: React.FC<{
month: "long",
day: "numeric",
};
const toLocaleDateString = useMemo(() => toLocaleDateStringFactory(locale), [locale]);

return (
<div
className={styles.taskListWrapper}
Expand Down Expand Up @@ -78,7 +91,7 @@ export const TaskListTableDefault: React.FC<{
maxWidth: rowWidth,
}}
>
&nbsp;{t.start.toLocaleDateString(locale, dateTimeOptions)}
&nbsp;{toLocaleDateString(t.start, dateTimeOptions)}
</div>
<div
className={styles.taskListCell}
Expand All @@ -87,12 +100,12 @@ export const TaskListTableDefault: React.FC<{
maxWidth: rowWidth,
}}
>
&nbsp;
{t.end.toLocaleDateString(locale, dateTimeOptions)}
&nbsp;{toLocaleDateString(t.end, dateTimeOptions)}
</div>
</div>
);
})}
</div>
);
};

17 changes: 16 additions & 1 deletion src/helpers/date-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Task, ViewMode } from "../types/public-types";
import DateTimeFormatOptions = Intl.DateTimeFormatOptions;
import DateTimeFormat = Intl.DateTimeFormat;

type DateHelperScales =
| "year"
Expand All @@ -9,6 +11,18 @@ type DateHelperScales =
| "second"
| "millisecond";

const intlDTCache = {};
const getCachedDateTimeFormat = (locString: string | string[], opts: DateTimeFormatOptions = {}): DateTimeFormat => {
const key = JSON.stringify([locString, opts]);
let dtf = intlDTCache[key];
if (!dtf) {
dtf = new Intl.DateTimeFormat(locString, opts);
intlDTCache[key] = dtf;
}
return dtf;
};


export const addToDate = (
date: Date,
quantity: number,
Expand Down Expand Up @@ -130,7 +144,7 @@ export const seedDates = (
};

export const getLocaleMonth = (date: Date, locale: string) => {
let bottomValue = new Intl.DateTimeFormat(locale, {
let bottomValue = getCachedDateTimeFormat(locale, {
month: "long",
}).format(date);
bottomValue = bottomValue.replace(
Expand Down Expand Up @@ -173,3 +187,4 @@ export const getWeekNumberISO8601 = (date: Date) => {
export const getDaysInMonth = (month: number, year: number) => {
return new Date(year, month + 1, 0).getDate();
};

0 comments on commit fd5f4ed

Please sign in to comment.