Performance: JSX vs HTML differences
MEDIUM IMPACT
This affects how the browser parses and renders UI elements, impacting initial load and update speed.
const element = <div className="container">Hello</div>; ReactDOM.render(element, root);
const htmlString = '<div class="container">Hello</div>'; ReactDOM.render(<div dangerouslySetInnerHTML={{__html: htmlString}} />, root);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Raw HTML string in React | Direct DOM insertion | Multiple reflows if content changes | High paint cost due to full DOM updates | [X] Bad |
| JSX with React.createElement | Virtual DOM diffing minimizes DOM ops | Single reflow on updates | Lower paint cost by updating only changed nodes | [OK] Good |