Avoid Layout Thrashing to keep your website performant

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

In web development, understanding the rendering process is important for creating fast, high-quality apps.
Layout reflow happens when browsers determine page element placement and size. However, if not managed properly, it can lead to performance issues.
Several actions can cause the browser to perform a reflow, including:
DOM Manipulation: JavaScript that changes the layout, like adjusting an element's height, can cause a reflow. This impacts the changed element, its child elements, and possibly its sibling and parent elements.
Style Changes: Adjusting styles that change the size or position of elements triggers a reflow.
Accessing Computed Styles: When your script accesses certain properties like offsetWidth, scrollTop, or calls getComputedStyle(), it can force a reflow because the browser must provide the most up-to-date value.
Layout thrashing happens when scripts keep switching between reading from and writing to the DOM. This makes the browser do many reflows quickly, one after another. This is not efficient and can make performance worse because each reflow uses a lot of computer power and takes up important CPU resources.
To minimize the impact of layout reflows, here is what you can do:
Batch DOM Read/Write Operations: Organize your code to group all DOM reads together before performing any writes. This approach reduces the number of reflows by avoiding repeated read/write cycles. For example, collect data from multiple elements first, then apply all your changes in one go.
Use Document Fragments: Don't change the DOM directly. Instead, use document fragments to make your changes off-DOM. When you're done, add the fragment to the DOM in one step. This way, you'll have fewer reflows.
Optimize CSS: Simplify your CSS selectors and avoid complex queries that can slow down style calculations and, by extension, reflows.
UserequestAnimationFrame: This API allows you to queue up changes to be executed in the next frame, aligning your updates with the browser's repaint cycle and reducing unnecessary reflows.
Debounce and Throttle Event Handlers: For events that cause many updates (like resize or scroll events), using debounce or throttle methods helps control how often event handlers run. This lowers the chance of layout problems.