0
0
Reactframework~8 mins

JSX vs HTML differences in React - Performance Comparison

Choose your learning style9 modes available
Performance: JSX vs HTML differences
MEDIUM IMPACT
This affects how the browser parses and renders UI elements, impacting initial load and update speed.
Rendering UI elements in React
React
const element = <div className="container">Hello</div>; ReactDOM.render(element, root);
JSX compiles to React.createElement calls, enabling React to build a virtual DOM efficiently and update only changed parts.
📈 Performance GainSingle virtual DOM creation, minimal reflows, faster updates
Rendering UI elements in React
React
const htmlString = '<div class="container">Hello</div>'; ReactDOM.render(<div dangerouslySetInnerHTML={{__html: htmlString}} />, root);
Passing raw HTML strings requires React to dangerously set innerHTML, causing slower parsing and potential security risks.
📉 Performance CostBlocks rendering while parsing HTML string, triggers extra DOM parsing and potential reflows
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Raw HTML string in ReactDirect DOM insertionMultiple reflows if content changesHigh paint cost due to full DOM updates[X] Bad
JSX with React.createElementVirtual DOM diffing minimizes DOM opsSingle reflow on updatesLower paint cost by updating only changed nodes[OK] Good
Rendering Pipeline
JSX is compiled to JavaScript calls that create virtual DOM nodes. React compares these nodes to update the real DOM efficiently, reducing costly layout and paint operations.
Parsing
Virtual DOM Creation
Diffing
Layout
Paint
⚠️ BottleneckParsing raw HTML strings and updating DOM directly
Core Web Vital Affected
LCP
This affects how the browser parses and renders UI elements, impacting initial load and update speed.
Optimization Tips
1Always use JSX in React to leverage virtual DOM for efficient updates.
2Avoid inserting raw HTML strings directly to prevent extra parsing and reflows.
3JSX improves Largest Contentful Paint by reducing direct DOM manipulations.
Performance Quiz - 3 Questions
Test your performance knowledge
Why is using JSX better than raw HTML strings in React for performance?
AJSX compiles to virtual DOM calls that minimize direct DOM updates
BJSX loads faster because it is plain HTML
CRaw HTML strings are faster because they skip React
DJSX reduces network requests
DevTools: Performance
How to check: Record a React render and look for scripting and rendering times; compare raw HTML string insertion vs JSX component render.
What to look for: Long scripting times and forced reflows indicate raw HTML string usage; efficient virtual DOM updates show shorter scripting and paint times.