IT lexicon › Web › Forced Layout

Forced Layout

Web På svenska → Updated: 2026-11-15

When JavaScript reads a layout property immediately after writing to the DOM, forcing the browser to recalculate layout synchronously.

If you write to the DOM (e.g., resize an element) and then immediately read its computed dimensions (e.g., element.offsetWidth), the browser must recalculate layout before the read—otherwise the value would be stale. This is called "forced synchronous layout" and is a major source of layout thrashing.

The solution is to read first, write later. Batch all reads in one loop, execute them, then batch writes together, and read again in the next loop. Or use a library method that buffers reads and writes automatically (e.g., FastDOM). This micro-optimization can save tens of milliseconds in performance-sensitive code.

← Back to the lexicon