Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created March 15, 2025 07:19
Show Gist options
  • Save Kcko/93d8eeeddcf8c805afda1d7e6fd95e6f to your computer and use it in GitHub Desktop.
Save Kcko/93d8eeeddcf8c805afda1d7e6fd95e6f to your computer and use it in GitHub Desktop.
/*
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