Ask any question about JavaScript here... and get an instant response.
How can I reduce DOM reflows when updating multiple elements in JavaScript?
Asked on Nov 25, 2025
Answer
To reduce DOM reflows when updating multiple elements, you can use techniques such as batching DOM updates, using DocumentFragment, or manipulating elements off-DOM. Here's a simple example using DocumentFragment to minimize reflows.
<!-- BEGIN COPY / PASTE -->
const fragment = document.createDocumentFragment();
for (let i = 0; i < 100; i++) {
const newElement = document.createElement('div');
newElement.textContent = `Item ${i}`;
fragment.appendChild(newElement);
}
document.getElementById('container').appendChild(fragment);
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- This example creates a DocumentFragment to batch the creation of 100 new "div" elements.
- The elements are appended to the fragment first, then the fragment is appended to the DOM in one operation.
- This approach reduces the number of reflows by updating the DOM once instead of 100 times.
Recommended Links:
