Performance: React vs traditional JavaScript
MEDIUM IMPACT
This affects how fast the page loads and updates when users interact, impacting rendering speed and responsiveness.
import React, { useState } from 'react'; function ItemList() { const [items, setItems] = useState([]); return ( <> <button onClick={() => setItems([...items, 'New item'])}>Add Item</button> <ul> {items.map((item, i) => <li key={i}>{item}</li>)} </ul> </> ); } export default ItemList;
const button = document.getElementById('btn'); button.addEventListener('click', () => { const list = document.getElementById('list'); const newItem = document.createElement('li'); newItem.textContent = 'New item'; list.appendChild(newItem); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Traditional JavaScript direct DOM manipulation | Multiple direct node insertions | 1 reflow per insertion | High paint cost per update | [X] Bad |
| React with virtual DOM and state updates | Batched DOM updates via virtual DOM | Single reflow per batch | Lower paint cost due to minimal changes | [OK] Good |