Read layout before applying grouped writes
Quick answer
Layout computes element geometry from styles and document structure. Reading geometry after style writes may require synchronous layout work. Group reads before writes when several independent measurements are needed.
How it works
element.getBoundingClientRect() returns viewport-relative geometry of a bounding rectangle including borders and padding; transforms can affect the result. offsetWidth is an integer layout width including padding and border (and scrollbar where applicable), excluding transforms. Style, layout, painting and compositing are distinct kinds of work, and browsers can optimize or skip stages. Interleaving writes and geometry reads can force recalculation repeatedly; capture the needed widths first, then apply changes. Measure traces to verify a benefit, not just code structure.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const box=document.createElement("div");box.style.cssText="width:60px;height:20px;padding:5px;border:1px solid";document.body.append(box);const rect=box.getBoundingClientRect();console.log(rect.width,rect.height);box.remove();})()Expected console output:
72 32Both dimensions include padding and borders in the fixture.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const nodes=[20,40].map(w=>{const el=document.createElement("div");el.style.width=w+"px";document.body.append(el);return el;});const widths=nodes.map(el=>el.getBoundingClientRect().width);nodes.forEach((el,i)=>el.style.width=(widths[i]+5)+"px");console.log(JSON.stringify(nodes.map(el=>el.getBoundingClientRect().width)));nodes.forEach(el=>el.remove());})()Expected console output:
[25,45]The two original widths are read before the writes; the final reads verify geometry, not speed.
Common mistakes
Do not call every geometry read slow or claim batching guarantees a fixed speedup. Avoid reading detached geometry as if the element were rendered.
Practice
Compare two list-update implementations in the profiler: alternating reads/writes and grouped reads then writes. Record actual layout events, not guessed speedups.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Read layout before applying grouped writes — Knowledge Check
Apply the concept and reason about local example output.
What coordinate reference does getBoundingClientRect use?
Why group independent geometry reads before style writes?
What demonstrates a real layout-performance improvement?
What is the exact console output in Read layout before applying grouped writes example 1?
(async () => {const box=document.createElement("div");box.style.cssText="width:60px;height:20px;padding:5px;border:1px solid";document.body.append(box);const rect=box.getBoundingClientRect();console.log(rect.width,rect.height);box.remove();})()What is the exact console output in Read layout before applying grouped writes example 2?
(async () => {const nodes=[20,40].map(w=>{const el=document.createElement("div");el.style.width=w+"px";document.body.append(el);return el;});const widths=nodes.map(el=>el.getBoundingClientRect().width);nodes.forEach((el,i)=>el.style.width=(widths[i]+5)+"px");console.log(JSON.stringify(nodes.map(el=>el.getBoundingClientRect().width)));nodes.forEach(el=>el.remove());})()