concept layout thrashing in category javascript

This is an excerpt from Manning's book Secrets of the JavaScript Ninja, Second Edition.
Because recalculating layout is expensive, browsers try to be as lazy as possible, by delaying working with the layout as much as they can; they try to batch as many write operations as possible on the DOM in a queue so that these operations can be executed in one go. Then, when an operation that requires an up-to-date layout comes along, the browser grudgingly obeys, and executes all batched operations and finally updates the layout. But sometimes, the way we’ll write our code doesn’t give the browser enough room to perform these sorts of optimizations, and we force the browser to perform a lot of (possibly needless) recalculations. This is what layout thrashing is all about; it occurs when our code performs a series of (often unnecessary) consecutive reads and writes to the DOM, not allowing the browser to optimize layout operations. The problem is that, whenever we modify the DOM, the browser has to recalculate the layout before any layout information is read. This action is expensive, in terms of performance. Let’s take a look at an example.
One way of minimizing layout thrashing is to write code in a way that doesn’t cause needless layout recalculations. For example, we can rewrite listing 12.10 into the following.
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 !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!