CSS3 & Modern Layouts (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What causes Forced Synchronous Layout (Layout Thrashing)?
senior LevelCode ExampleSystem Design
In Simple Words: Occurs in JavaScript when code repeatedly reads layout properties (geometry) immediately after writing style changes:
javascript
// ❌ LAYOUT THRASHING: Forces browser to calculate reflow on every loop iteration!
elements.forEach(el => {
const width = el.offsetWidth; // READ (Forces reflow)
el.style.width = `${width + 10}px`; // WRITE (Invalidates layout)
});
// ✅ BATCHED READS & WRITES: Single reflow calculation
const widths = elements.map(el => el.offsetWidth); // Batch READs first
elements.forEach((el, i) => {
el.style.width = `${widths[i] + 10}px`; // Batch WRITEs last
});