← Back to JavaScript
28

DOM performance: avoid reflows and unnecessary work

Learn how to reduce rendering cost by batching DOM changes and minimizing conflicting layout reads and writes.

📘 Theory

What layout thrashing looks like

Reading measurements and writing styles in the same loop creates avoidable cost.

1

If you ask for `offsetHeight` or `getBoundingClientRect()` and immediately mutate styles on every iteration, the browser may recalculate layout again and again.

2

That pattern becomes especially visible in long lists, repeated filters and animation-like updates.

Batch reads and writes

Measure first, then apply changes.

One practical improvement is to collect layout information in one pass and apply style changes in a second pass.

That keeps reads and writes grouped, which usually reduces forced layout recalculation.

  • Avoid `innerHTML +=` inside loops.
  • If you create many nodes, build them in a fragment first.
  • Prefer CSS classes applied in groups over many ad hoc inline styles.

Use fragments and `requestAnimationFrame()` wisely

Two simple tools can make heavy DOM updates much cleaner.

1

`DocumentFragment` lets you prepare a structure in memory and append it in one step.

2

`requestAnimationFrame()` helps you coordinate visual work with the browser paint cycle when you need to react to resize or animation-like updates.

🧪 Learn by doing

Example Guided example: efficient large-list rendering Generate a big list with a fragment instead of appending visible nodes one by one.

🏁 Challenges

Challenge Challenge: replace incremental `innerHTML +=` Rewrite a loop with repeated `innerHTML +=` into a single `map().join('')` assignment.

🧰 Resources

Test

Check your knowledge with a test about JavaScript.

Test for JavaScript

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com