Performance: Dark mode toggle with JavaScript
MEDIUM IMPACT
This affects page interactivity and visual stability when toggling themes.
const toggleDarkMode = () => {
document.documentElement.classList.toggle('dark');
};
button.addEventListener('click', toggleDarkMode);const toggleDarkMode = () => {
document.body.style.backgroundColor = '#000';
document.body.style.color = '#fff';
};
button.addEventListener('click', toggleDarkMode);| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline style changes per property | Multiple style updates on body | Multiple reflows triggered | High paint cost due to many changes | [X] Bad |
| Toggle single CSS class on root | One class toggle on <html> | Single reflow triggered | Minimal paint cost | [OK] Good |