Guides

Performance

Where Time does its work, and how to keep that work small.

Time does its work in two steps, and caches each one:

  1. Reading events. Root runs your records through the adapter into a snapshot. This happens again only when the records change.
  2. Projecting the view. Root calls the active view's project with the snapshot and the visible range. This happens again only when the snapshot, range, options, resources, environment, or today's date change.

Everything else is a normal React render. These habits keep both steps rare and cheap.

Replace records, don't mutate them

Root compares events item by item. A new array with the same record objects in the same order counts as unchanged, so you can filter or map inline, as in events={meetings.filter((meeting) => !meeting.cancelled)}.

When the array changes, Time reads every record through the adapter again, except those with an unchanged revision; if every record comes out the same, in the same order, it keeps its interval indexes. Update records immutably: replace the changed record with a new object. A record changed in place keeps its identity, so if nothing else in the array changed, the item-by-item comparison sees no change and the calendar doesn't update.

Add a revision to your adapter

When you refetch, your data layer usually creates new objects for every record, even unchanged ones. getRevision lets Time skip those. Return anything that changes whenever the record changes, such as an updatedAt timestamp or a version number:

const bookingAdapter = defineEventAdapter<Booking>()({
  getId: (booking) => booking.id,
  getTitle: (booking) => booking.summary,
  getInterval: (booking) => ({ kind: "timed", start: booking.startsAt, end: booking.endsAt }),
  getResourceIds: (booking) => booking.roomIds,
  getRevision: (booking) => booking.updatedAt,
});

Records with the same ID and revision as last time reuse their earlier result. The revision must change whenever anything the adapter reads changes; otherwise Time keeps showing the old title or time.

Do the heavy work in project

A view's project runs only when its inputs change, while its Renderer re-renders like any component. Put filtering, sorting, and layout in project, and keep the Renderer to drawing.

Inside project, ask the snapshot for the visible range instead of looping over every event:

project: ({ snapshot, instantRange }) => snapshot.queryTimed(instantRange),

Queries use an interval index, so they don't scan every event. Each snapshot also remembers the results of its last 128 distinct queries, so asking again for the same range returns the same array.

Keep functions in options stable

Root compares locale, weekRules, now and a controlled anchorDate, timeZone or view by value, and each view's options key by key, so an inline options object is fine. Its values are compared by identity, though: a new function or nested object, such as a time grid's annotations, is a new value.

A getEventColor written inline in viewOptions makes the view project again on every render of the component that renders Root. Define it once instead:

// At module scope, outside the component that renders Root:
const getEventColor = (meeting: Meeting) => (meeting.title === "Standup" ? "green" : "blue");

// Then, on Root:
viewOptions={{ agenda: { getEventColor } }}

The same goes for a clock object. Change handlers and onDiagnosticsChange can be inline: Root reads them when it needs them.

Subscribe to what you use

useCalendarState() and useCalendar() re-render their component whenever any state changes. For a component that needs one value, use a selector:

function ViewName() {
  const view = Calendar.useCalendarSelector((state) => state.view);
  return <span>{view}</span>;
}

The component re-renders only when the selected value changes. If your selector builds a new object each time, pass isEqual so equal results don't count as changes:

const position = Calendar.useCalendarSelector(
  (state) => ({ view: state.view, anchorDate: state.anchorDate }),
  { isEqual: (a, b) => a.view === b.view && a.anchorDate.equals(b.anchorDate) },
);

On this page