Layout Thrashing
Overview
Layout thrashing is a performance issue that arises when code performs unnecessary consecutive reads and writes to the Document Object Model (DOM). This forces the browser to repeatedly recalculate the layout, which can significantly degrade performance. The key to minimizing layout thrashing is to batch DOM reads and writes, allowing the browser to optimize layout operations more effectively.
Definition
Layout thrashing occurs when a series of consecutive reads and writes to the DOM are performed. This prevents the browser from optimizing layout operations, leading to performance issues. Whenever the DOM is modified, the browser must recalculate the layout before any layout information is read, which is an expensive operation in terms of performance.
Example of Layout Thrashing
Consider the following example, which demonstrates layout thrashing by performing consecutive reads and writes to the DOM:
Listing 12.10. Consecutive series of reads and writes causes layout thrashing
<div id="ninja">I’m a ninja</div> #A
<div id="samurai">I’m a samurai</div> #A
<div id="ronin">I’m a ronin</div> #A
<script>
const ninja = document.getElementById("ninja"); #B
const samurai = document.getElementById("samurai"); #B
const ronin = document.getElementById("ronin"); #B
const ninjaWidth = ninja.clientWidth; #C
ninja.style.width = ninjaWidth/2 + "px"; #C
#C
const samuraiWidth = samurai.clientWidth; #C
samurai.style.width = samuraiWidth/2 + "px"; #C
#C
const roninWidth = ronin.clientWidth; #C
ronin.style.width = roninWidth/2 + "px"; #C
</script>
#A - Defines a few HTML elements
#B - Fetches the elements from the DOM
#C - Performs a series of consecutive reads and writes. DOM modifications invalidate the layout.
In this example, reading the value of an element’s clientWidth property requires the browser to have an up-to-date layout. By performing consecutive reads and writes to the width property of different elements, the browser is forced to recalculate the layout every time, ensuring that the correct information is obtained.
Minimizing Layout Thrashing
To minimize layout thrashing, code should be written in a way that avoids unnecessary layout recalculations. One effective strategy is to batch DOM reads and writes. This approach allows the browser to batch operations that modify the DOM, reducing the need for repeated layout recalculations.
Listing 12.11. Batch DOM reads and writes to avoid layout thrashing
<div id="ninja">I’m a ninja</div>
<div id="samurai">I’m a samurai</div>
<div id="ronin">I’m a ronin</div>
<script>
const ninja = document.getElementById("ninja");
const samurai = document.getElementById("samurai");
const ronin = document.getElementById("ronin");
const ninjaWidth = ninja.clientWidth; #A
const samuraiWidth = samurai.clientWidth; #A
const roninWidth = ronin.clientWidth; #A
ninja.style.width = ninjaWidth/2 + "px"; #B
samurai.style.width = samuraiWidth/2 + "px"; #B
ronin.style.width = roninWidth/2 + "px"; #B
</script>
#A - Batches all reads to layout properties together
#B - Batches all writes to layout properties together
In this improved example, all reads to layout properties are batched together, followed by all writes. This method is effective because there are no dependencies between the dimensions of the elements; setting the width of the ninja element does not influence the width of the samurai element. By batching these operations, the browser can optimize the layout recalculations, improving performance.