perf: Faster logic by preventing instanceof Dayjs in slots.ts (#22590)
* perf: Faster logic by preventing instanceof Dayjs in slots.ts * Flip isSameOrAfter to isBefore as .. before
This commit is contained in:
+12
-11
@@ -69,7 +69,7 @@ function buildSlotsWithDateRanges({
|
||||
|
||||
const startTimeWithMinNotice = dayjs.utc().add(minimumBookingNotice, "minute");
|
||||
|
||||
const slotBoundaries = new Map<string, true>();
|
||||
const slotBoundaries = new Map<number, true>();
|
||||
|
||||
orderedDateRanges.forEach((range) => {
|
||||
const dateYYYYMMDD = range.start.format("YYYY-MM-DD");
|
||||
@@ -86,23 +86,24 @@ function buildSlotsWithDateRanges({
|
||||
slotStartTime = slotStartTime.add(offsetStart ?? 0, "minutes").tz(timeZone);
|
||||
|
||||
// Find the nearest appropriate slot boundary if this time falls within an existing slot
|
||||
const slotBoundariesArray = Array.from(slotBoundaries.keys()).map((t) => dayjs(t));
|
||||
if (slotBoundariesArray.length > 0) {
|
||||
slotBoundariesArray.sort((a, b) => a.valueOf() - b.valueOf());
|
||||
const slotBoundariesValueArray = Array.from(slotBoundaries.keys());
|
||||
if (slotBoundariesValueArray.length > 0) {
|
||||
slotBoundariesValueArray.sort((a, b) => a - b);
|
||||
|
||||
let prevBoundary = null;
|
||||
for (let i = slotBoundariesArray.length - 1; i >= 0; i--) {
|
||||
if (slotBoundariesArray[i].isBefore(slotStartTime)) {
|
||||
prevBoundary = slotBoundariesArray[i];
|
||||
for (let i = slotBoundariesValueArray.length - 1; i >= 0; i--) {
|
||||
if (slotBoundariesValueArray[i] < slotStartTime.valueOf()) {
|
||||
prevBoundary = slotBoundariesValueArray[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (prevBoundary) {
|
||||
const prevBoundaryEnd = prevBoundary.add(frequency + (offsetStart ?? 0), "minutes");
|
||||
const prevBoundaryEnd = dayjs(prevBoundary).add(frequency + (offsetStart ?? 0), "minutes");
|
||||
if (prevBoundaryEnd.isAfter(slotStartTime)) {
|
||||
if (!prevBoundary.isBefore(range.start)) {
|
||||
slotStartTime = prevBoundary;
|
||||
const dayjsPrevBoundary = dayjs(prevBoundary);
|
||||
if (!dayjsPrevBoundary.isBefore(range.start)) {
|
||||
slotStartTime = dayjsPrevBoundary;
|
||||
} else {
|
||||
slotStartTime = prevBoundaryEnd;
|
||||
}
|
||||
@@ -118,7 +119,7 @@ function buildSlotsWithDateRanges({
|
||||
continue;
|
||||
}
|
||||
|
||||
slotBoundaries.set(slotKey, true);
|
||||
slotBoundaries.set(slotStartTime.valueOf(), true);
|
||||
|
||||
const dateOutOfOfficeExists = datesOutOfOffice?.[dateYYYYMMDD];
|
||||
let slotData: {
|
||||
|
||||
Reference in New Issue
Block a user