Your Per-Item ResizeObserver Is a Layout Feedback Loop
Every virtualized list with variable-height rows eventually meets the same bug: you add a ResizeObserver to each row so you can measure it, write the measured height back into your layout state, and the page starts fighting the user's scroll. GitHub's diff-surface rebuild hit exactly this, and their fix generalizes to any large list. By the end you will know how to detect the feedback loop, how…
Virtualized lists with variable-height rows often encounter a common bug: adding a ResizeObserver to each row for measurement leads to a layout feedback loop, causing the page to fight user scrolling. GitHub's diff-surface rebuild addresses this issue and can be applied to any large list. This article explains how to detect the feedback loop, batch measurements, and anchor height corrections to prevent content movement.
Pattern of the feedback loop:
1. Virtualize a list; each row requires a measured height.
2. Attach a ResizeObserver per row and write the height into the virtualizer's state.
3. Measurement triggers layout, which in turn triggers measurement, leading to performance degradation.
To prevent this:
1. Separate deterministic elements (fixed-height items) from dynamic blocks (height depends on content, open state, and container width).
2. Compute deterministic blocks upfront and only measure dynamic blocks when necessary.
Key points:
1. Use a Set to track dirty blocks containing dynamic heights. Only measure when the fingerprint of changes changes.
2. Batch measurements using requestIdleCallback and scope the pass to blocks near the viewport.
3. Apply corrections in a single layout pass to avoid content jumping under the user's cursor.
4. Anchor corrections to what the user sees by adjusting the scroll offset for blocks above the viewport and letting layouts absorb changes for blocks below.
5. Provide a synchronous escape hatch for user-initiated dimension changes, such as toggling collapsible sections or opening reply composers.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.