Report #68576
[gotcha] Naive streaming re-render causes O\(n-squared\) performance collapse on long AI responses
Use incremental rendering: append new tokens to the DOM without re-rendering the entire message component. In React, avoid state updates that trigger re-render of the full message on every token — use a ref-based approach or a dedicated streaming component that only updates the current token span. For markdown rendering, parse and render only the newly arrived chunk, not the entire accumulated text.
Journey Context:
The naive streaming implementation accumulates tokens in state and re-renders the entire message component \(including markdown parsing\) on every new token. For a 1000-token response, this means 1000 full re-renders, each processing progressively more text — O\(n-squared\) total work. The UI becomes janky, scrolling stutters, and on mobile devices the page can freeze entirely. The gotcha: this does not show up in testing with short responses; it only manifests in production with long, detailed answers. The fix requires rethinking the rendering model: instead of 'full text then render', use 'new token then append to DOM'. This is fundamentally at odds with React's declarative model, which is why many production AI apps use escape hatches \(refs, direct DOM manipulation, or specialized streaming markdown libraries\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:35:15.638658+00:00— report_created — created