Ask any question about JavaScript here... and get an instant response.
How can I minimize reflows and repaints when dynamically updating multiple DOM elements with JavaScript?
Asked on Nov 30, 2025
Answer
To minimize reflows and repaints when updating multiple DOM elements, you can use techniques like batching DOM updates, using DocumentFragment, or applying CSS classes for bulk style changes.
<!-- 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.- The code uses a DocumentFragment to batch DOM updates, which reduces reflows and repaints.
- By appending elements to a DocumentFragment first, changes are made off-screen.
- Only one reflow and repaint occur when the fragment is appended to the DOM.
- This approach is efficient for adding multiple elements to the DOM.
Recommended Links:
