Context
Chatto's frontend uses Svelte 5 runes ($state, $derived) for reactive state management. The room event stores receive realtime events and projected timeline refreshes, then cache renderable events for child components to consume.
A performance investigation revealed that pushing any item to a $state array triggers every $derived expression that reads it. Even if downstream $derived chains immediately filter the item out (e.g., filtering by room ID), the entire chain re-evaluates because .filter() always produces a new array reference. This caused CPU spikes when high-frequency events like typing indicators were added to the cache.
The initial approach used a blacklist: cache everything, then filter out unwanted event types downstream. This failed because the damage (triggering the reactive chain) was already done by the time the filter ran.
Decision
Use a whitelist of cacheable event types at the cache entry point. Events that are not in the whitelist are never added to the $state array:
- Only events that components need for rendering (messages, joins, leaves, edits, deletions) are added to the cache
- Ephemeral signal events such as typing indicators and presence updates use separate, targeted reactive state. Durable reactions arrive through projection rows and enter the render cache only through the explicit kind gate.
- The whitelist is checked before the
$statearray mutation, not after
Additionally:
- Cache expensive object construction (e.g.,
Intl.DateTimeFormatinstances) at module level rather than creating them inline on every render - When grouping or transforming cached data, ensure intermediate
$derivedsteps don't create unnecessary new references
Consequences
- No wasted reactive cycles: Events that won't survive downstream filters never enter the reactive graph. The
$derivedchain only re-evaluates when genuinely relevant data changes. - Explicit cache contract: The whitelist makes it clear which events are cached and which are ephemeral. Adding a new event type to the cache is a conscious decision.
- Ephemeral events need separate handling: Typing indicators, audio levels, presence, and similar high-frequency signals must use their own state rather than the general event cache. Durable reaction rows follow the projected timeline path.
- Svelte 5 reactivity is reference-based: This is a fundamental characteristic of the framework. Any mutation to a
$statevalue triggers all readers, regardless of whether the logical content changed. All reactive state design must account for this. - New event types require a decision: When adding a new event type, developers must decide whether it goes in the cache whitelist or gets separate handling. This is additional friction but prevents performance regressions.