Skip to content

Commit ac117a1

Browse files
authored
perf: Faster logic by preventing instanceof Dayjs in slots.ts (calcom#22590)
* perf: Faster logic by preventing instanceof Dayjs in slots.ts * Flip isSameOrAfter to isBefore as .. before
1 parent f85f226 commit ac117a1

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

packages/lib/slots.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function buildSlotsWithDateRanges({
6969

7070
const startTimeWithMinNotice = dayjs.utc().add(minimumBookingNotice, "minute");
7171

72-
const slotBoundaries = new Map<string, true>();
72+
const slotBoundaries = new Map<number, true>();
7373

7474
orderedDateRanges.forEach((range) => {
7575
const dateYYYYMMDD = range.start.format("YYYY-MM-DD");
@@ -86,23 +86,24 @@ function buildSlotsWithDateRanges({
8686
slotStartTime = slotStartTime.add(offsetStart ?? 0, "minutes").tz(timeZone);
8787

8888
// Find the nearest appropriate slot boundary if this time falls within an existing slot
89-
const slotBoundariesArray = Array.from(slotBoundaries.keys()).map((t) => dayjs(t));
90-
if (slotBoundariesArray.length > 0) {
91-
slotBoundariesArray.sort((a, b) => a.valueOf() - b.valueOf());
89+
const slotBoundariesValueArray = Array.from(slotBoundaries.keys());
90+
if (slotBoundariesValueArray.length > 0) {
91+
slotBoundariesValueArray.sort((a, b) => a - b);
9292

9393
let prevBoundary = null;
94-
for (let i = slotBoundariesArray.length - 1; i >= 0; i--) {
95-
if (slotBoundariesArray[i].isBefore(slotStartTime)) {
96-
prevBoundary = slotBoundariesArray[i];
94+
for (let i = slotBoundariesValueArray.length - 1; i >= 0; i--) {
95+
if (slotBoundariesValueArray[i] < slotStartTime.valueOf()) {
96+
prevBoundary = slotBoundariesValueArray[i];
9797
break;
9898
}
9999
}
100100

101101
if (prevBoundary) {
102-
const prevBoundaryEnd = prevBoundary.add(frequency + (offsetStart ?? 0), "minutes");
102+
const prevBoundaryEnd = dayjs(prevBoundary).add(frequency + (offsetStart ?? 0), "minutes");
103103
if (prevBoundaryEnd.isAfter(slotStartTime)) {
104-
if (!prevBoundary.isBefore(range.start)) {
105-
slotStartTime = prevBoundary;
104+
const dayjsPrevBoundary = dayjs(prevBoundary);
105+
if (!dayjsPrevBoundary.isBefore(range.start)) {
106+
slotStartTime = dayjsPrevBoundary;
106107
} else {
107108
slotStartTime = prevBoundaryEnd;
108109
}
@@ -118,7 +119,7 @@ function buildSlotsWithDateRanges({
118119
continue;
119120
}
120121

121-
slotBoundaries.set(slotKey, true);
122+
slotBoundaries.set(slotStartTime.valueOf(), true);
122123

123124
const dateOutOfOfficeExists = datesOutOfOffice?.[dateYYYYMMDD];
124125
let slotData: {

0 commit comments

Comments
 (0)