[@mantine/schedule] MonthView: rendering time grows quadratically with the number of events in a week
Dependencies check up
- I have verified that I use latest version of all @mantine/* packages
What version of @mantine/* packages do you have in package.json?
9.6.3 (also reproduced on 9.6.2)
What package has an issue?
@mantine/schedule
What framework do you use?
Reproduced in plain Node.js (v26), independent of the framework
In which browsers you can reproduce the issue?
All (the cost is in pure JS; it also blocks server-side rendering)
Describe the bug
MonthView rendering time grows quadratically with the number of events in a single week. Rendering is synchronous, so the main thread (or the Node.js process during SSR) is blocked for the whole duration.
Time spent in getMonthViewEvents for one month with N one-hour events (min of 3 runs, plain Node.js):
| Events | All on the same day | Spread over one week |
|---|---|---|
| 1 000 | 2.3 s | 2.7 s |
| 2 000 | 9.2 s | 10.5 s |
| 2 400 | 13.4 s | 13.8 s |
Reproduction:
import { renderToString } from 'react-dom/server';
import { MantineProvider } from '@mantine/core';
import { MonthView } from '@mantine/schedule';
const events = Array.from({ length: 2000 }, (_, id) => ({
id,
title: `Event ${id}`,
color: 'blue',
start: '2025-01-15 10:00:00',
end: '2025-01-15 11:00:00',
}));
console.time('render');
renderToString(
<MantineProvider>
<MonthView date="2025-01-01" events={events} />
</MantineProvider>
);
console.timeEnd('render'); // ~9.2 sThe timings in the table were measured on getMonthViewEvents (the function MonthView uses to position events), where almost all of the render time is spent.
Cause
getMonthPositionedEvents calls findAvailableRow once per event and per week, passing every event already placed in that week:
const row = findAvailableRow({
existingEvents: groupedByWeek[weekIdx.toString()],
startDayIndex,
daysSpanned,
weekStart,
});findAvailableRow scans all of them, so placing the events of one week costs O(n²). Each comparison also re-parses existing.start with dayjs() and calls startOf, isBefore, isSame and diff, which makes the constant factor high.
If possible, include a link to a codesandbox with a minimal reproduction
See the snippet above.
Possible fix
The row chosen for an event is 1 + max(row) over the already placed events that share at least one day with it. A week has only 7 days, so the same result can be computed from a per-week array nextRowByDay[7] (first free row for each day):
row = max(nextRowByDay[startDayIndex .. startDayIndex + daysSpanned - 1])- then
nextRowByDay[d] = row + 1for each of those days
This is O(7) per event instead of O(n) and needs no date parsing. It gives exactly the same rows as the current scan: position.width is daysSpanned / 7 * 100 (clamped to the week for hanging events), and (width / 100) * 7 is exactly daysSpanned for 1..7 in floating point.
With this change, the same benchmark takes 140 ms for 2 400 events (about 100× faster) and the remaining cost is linear. The renderToString reproduction above goes from 9.2 s to 0.26 s.
Related issues found while investigating
These are not caused by the quadratic scan and the proposed fix keeps the current behavior for them. I can open separate issues if that is preferred.
WeekView/DayViewhave similar quadratic patterns:assignEventRows(all-day rows) rescans every placed event withisEventsOverlapfor each row it tries.findAvailableColumn,calculateRegularEventOverlapsand the overlap pass ingetDayPositionedEventscompare each event with all others, the last one up to O(n³) for a single cluster.- Month rows are never reused: an event takes the row after the highest row already used on any of its days. With
maxEventsPerDay={3}, three Monday events (rows 0-2), then a Monday-Sunday event (row 3), then three Tuesday events (rows 4-6): Tuesday shows no event and "+1 more" although four events are hidden. - An event whose start and end are both at midnight (
start === end === '2025-01-15 00:00:00') gets an end day before its start day. On the first day of a week it is dropped. Mid-week it takes a row ingroupedByWeekbut is not added togroupedByDay, so it is not rendered and pushes the following events of that day down one row. - The range filter in
getMonthViewEventsuses||(end.isAfter(range.start, 'day') || start.isBefore(range.end, 'day')), which is true for almost every event, so events far outside the visible month still go through the positioning loop. MonthViewcallsgetMonthViewEventson every render without memoization, so hover/drag state changes recompute all positions.
Self-service
- I would be willing to implement a fix for this issue
Source: mantinedev/mantine