Performance: Why patterns improve architecture
MEDIUM IMPACT
This concept affects how efficiently the browser renders and updates the UI by organizing code and components for better reuse and predictability.
import React from 'react'; function Time() { const [time, setTime] = React.useState(new Date()); React.useEffect(() => { const id = setInterval(() => setTime(new Date()), 1000); return () => clearInterval(id); }, []); return <div>{time.toLocaleTimeString()}</div>; } export default function Page() { const [count, setCount] = React.useState(0); return ( <div> <button onClick={() => setCount(count + 1)}>Increment</button> <p>{count}</p> <Time /> </div> ); }
import React from 'react'; export default function Page() { const [count, setCount] = React.useState(0); const [time, setTime] = React.useState(new Date()); React.useEffect(() => { const id = setInterval(() => setTime(new Date()), 1000); return () => clearInterval(id); }, []); return ( <div> <button onClick={() => setCount(count + 1)}>Increment</button> <p>{count}</p> <div>{time.toLocaleTimeString()}</div> </div> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Monolithic component with mixed concerns | High (updates entire tree) | Multiple per second | High (full component repaint) | [X] Bad |
| Component separation with isolated state | Low (updates small subtree) | Single per second | Low (small repaint area) | [OK] Good |