{"success":true,"name":"batch-dom-reads-and-writes-alternating-them-forces-the-brows","description":"A loop that measures elements and changes styles item by item feels slow","category":"ai-brimind","path":"ai-brimind/batch-dom-reads-and-writes-alternating-them-forces-the-brows/SKILL.md","file":"SKILL.md","content":"---\nname: batch-dom-reads-and-writes-alternating-them-forces-the-brows\ndescription: \"A loop that measures elements and changes styles item by item feels slow\"\n---\n\n# Batch DOM Reads and Writes: Alternating Them Forces the Browser to Re-Layout Repeatedly\n\n## When to use\nFrontend code interleaves reading layout values (sizes, positions, offsets) with writing styles or DOM changes, especially inside loops over many elements.\n\n## Example request\nloop: read each row's height, then set its width → every iteration forces a fresh layout; thrashing loop. style written, then offset read immediately → the read makes the browser compute layout right now; forced synchronous layout. resizing 200 items one by one → 200 layout passes instead of one; unbatched writes. — each ping-pongs between asking and changing.\n\n## Instructions\nAct as a renovator who first measures every room, then does all the construction — never measuring, hammering, measuring, hammering. The browser postpones layout work until it must answer a question: reading a size or position after a change forces it to compute the whole layout immediately, and doing that inside a loop forces it once per iteration — the classic invisible slowdown. Structure DOM work in phases: first collect every measurement you need into variables, then perform all the writes using those stored values, so the browser lays out once instead of hundreds of times. For changes across many elements, prefer one change at the container level (a class toggle, a CSS variable) over per-element style writes. Schedule visual updates with the browser's frame callback when animating, and remember the cheap properties: transforms and opacity change without triggering layout at all. When a screen stutters during updates, hunt the read-after-write pattern before blaming data size. Only proceed once every DOM-heavy routine reads in one pass and writes in another — measured, then built.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #745\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}