DOM operations are slow compared to JavaScript. Optimize by:
- Minimizing DOM queries (cache references)
- Batching updates with fragments
- Using
textContentoverinnerHTML - Avoiding layout thrashing (read, then write)
// Bad: query each time
for (...) document.querySelector(".x").style...
// Good: cache reference
const el = document.querySelector(".x");
for (...) el.style...