The final letter of RADIO. Performance optimization in a streaming chat interface is about maintaining 60 fps while tokens arrive at unpredictable rates. Every optimization below was either used in the real ChatGPT frontend or is a standard pattern for streaming SPAs.
1. Selective subscriptions with Zustand
Every call to appendToken() updates the message content in the store. If the Sidebar is subscribed to the entire store, it re-renders on every token. Solution: each component subscribes only to the slice it needs. The MessageBubble subscribes to its own message content via a selector. The Sidebar subscribes only to the conversation summary list. Zustand's equality check ensures that a component only re-renders when its selected values change.
2. Debounced markdown rendering
As discussed in the streaming section, markdown is not parsed mid-stream. But even on finalize, parsing a 10,000-word response with nested code blocks can take 10-50ms. This work is scheduled via requestIdleCallback() or deferred to a microtask, ensuring the status transition ("streaming" → "complete") renders immediately and the markdown processor runs when the browser is idle.
3. Code syntax highlighting off the main thread
Syntax highlighting is CPU-intensive. A code block with 200 lines of TypeScript can take 30-80ms to tokenize. Highlighting should run in a Web Worker. The worker receives raw code and a language identifier, returns HTML tokens, and the main thread applies the result. The worker is created lazily — only when a code block is encountered — so conversations without code don't pay the worker initialization cost.
4. Optimistic updates for non-streaming actions
Not every action needs a server round trip. Renaming a conversation, deleting a message, or toggling a model selector are applied immediately to the local store and synced to the server asynchronously. If the server rejects the update (e.g., conversation not found), the store rolls back and shows an error toast.
5. Lazy loading heavy dependencies
The markdown parser (marked), syntax highlighter (shiki orhighlight.js), and LaTeX renderer (katex) are each 50-200 KB gzipped. They are loaded via dynamic import() only when the first message completes streaming. The initial bundle is just the SPA shell, Zustand, and the virtual scroller — roughly 120 KB gzipped.
Tradeoff table
| Strategy | Gain | Cost |
|---|
| Zustand selectors | Sub-millisecond re-render scoping | Boilerplate for selector hooks |
| Deferred markdown | No mid-stream flicker | Brief flash of raw text on finalize |
| Web Worker highlighting | Main thread never blocks >5ms | ~200ms worker init latency (first code block) |
| Virtual scroll | ~30 DOM nodes regardless of conversation length | Variable height measurement complexity |
| Optimistic updates | Instant UI feedback for mutations | Rollback logic on server rejection |