perf: Use Intl.DateTimeFormat instead of Dayjs.tz().format() (#10604)

pull/10612/head
Alex van Andel 2023-08-05 07:44:47 +01:00 committed by GitHub
parent 359779cabb
commit 9a08fbb22d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 4 deletions

View File

@ -408,17 +408,27 @@ export async function getSchedule(input: TGetScheduleInputSchema) {
}
availableTimeSlots = availableTimeSlots.filter((slot) => isTimeWithinBounds(slot.time));
// fr-CA uses YYYY-MM-DD
const formatter = new Intl.DateTimeFormat("fr-CA", {
year: "numeric",
month: "2-digit",
day: "2-digit",
timeZone: input.timeZone,
});
const computedAvailableSlots = availableTimeSlots.reduce(
(
r: Record<string, { time: string; users: string[]; attendees?: number; bookingUid?: string }[]>,
{ time: _time, ...passThroughProps }
{ time, ...passThroughProps }
) => {
// TODO: Adds unit tests to prevent regressions in getSchedule (try multiple timezones)
const time = _time.tz(input.timeZone);
r[time.format("YYYY-MM-DD")] = r[time.format("YYYY-MM-DD")] || [];
r[time.format("YYYY-MM-DD")].push({
// This used to be _time.tz(input.timeZone) but Dayjs tz() is slow.
// toLocaleDateString slugish, using Intl.DateTimeFormat we get the desired speed results.
const dateString = formatter.format(time.toDate());
r[dateString] = r[dateString] || [];
r[dateString].push({
...passThroughProps,
time: time.toISOString(),
users: (eventType.hosts