Created
March 15, 2025 07:19
-
-
Save Kcko/93d8eeeddcf8c805afda1d7e6fd95e6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Why It’s a Problem: | |
Direct DOM manipulation triggers reflows and repaints, slowing down rendering. | |
Inserting elements one by one instead of batching updates increases the number of re-renders. | |
Modifying styles directly forces layout recalculations. | |
*/ | |
// BAD: Multiple reflows | |
for (let i = 0; i < 1000; i++) { | |
const div = document.createElement('div'); | |
div.textContent = `Item ${i}`; | |
document.body.appendChild(div); | |
} | |
// GOOD: Batch updates using DocumentFragment | |
const fragment = document.createDocumentFragment(); | |
for (let i = 0; i < 1000; i++) { | |
const div = document.createElement('div'); | |
div.textContent = `Item ${i}`; | |
fragment.appendChild(div); | |
} | |
document.body.appendChild(fragment); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment