Performance: Composition vs inheritance
MEDIUM IMPACT
This concept affects how components are structured and rendered, impacting bundle size, rendering speed, and update efficiency.
function Button({ label, children }) { return <button>{children || label}</button>; } function IconButton(props) { return <Button {...props}><Icon />{props.label}</Button>; }
class Button extends React.Component { render() { return <button>{this.props.label}</button>; } } class IconButton extends Button { render() { return <button><Icon />{this.props.label}</button>; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inheritance with deep class hierarchy | High due to nested components | Multiple reflows per inheritance level | Higher paint cost due to complex tree | [X] Bad |
| Composition with functional components | Lower, flat component tree | Single reflow per update | Lower paint cost with optimized updates | [OK] Good |