0
0
Reactframework~8 mins

Synthetic events in React - Performance & Optimization

Choose your learning style9 modes available
Performance: Synthetic events
MEDIUM IMPACT
Synthetic events affect how user interactions are handled and can impact input responsiveness and event processing speed.
Handling user clicks efficiently in React
React
function Button() {
  return <button onClick={() => console.log('Clicked')}>Click me</button>;
}
Avoids unnecessary event persistence, allowing React to reuse event objects and optimize memory.
📈 Performance GainReduces memory overhead and keeps event handling fast and responsive.
Handling user clicks efficiently in React
React
function Button() {
  return <button onClick={(e) => { e.persist(); console.log('Clicked'); }}>Click me</button>;
}
Calling e.persist() disables React's event pooling, increasing memory usage and delaying garbage collection.
📉 Performance CostIncreases memory usage and can cause minor delays in event processing under heavy interaction.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Using React synthetic events normallyNo extra DOM ops00[OK] Good
Calling e.persist() on synthetic eventsNo extra DOM ops00[!] OK - higher memory use
Using native DOM events directly in ReactNo extra DOM ops00[!] OK - bypasses React pooling
Heavy event handler logic inside synthetic eventsNo extra DOM ops00[X] Bad - slows input responsiveness
Rendering Pipeline
Synthetic events are created by React to normalize events across browsers. When a user interacts, React captures the native event, wraps it in a synthetic event, and passes it to handlers. This adds a small step before the event reaches your code.
Event Handling
JavaScript Execution
⚠️ BottleneckJavaScript Execution due to event object creation and pooling management
Core Web Vital Affected
INP
Synthetic events affect how user interactions are handled and can impact input responsiveness and event processing speed.
Optimization Tips
1Avoid calling e.persist() unless you need to keep the event beyond the handler.
2Keep event handlers simple and fast to improve input responsiveness.
3Use React synthetic events to benefit from cross-browser normalization with minimal overhead.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance cost of React synthetic events compared to native events?
AThey add a small JavaScript processing step to normalize events.
BThey cause extra DOM reflows on every event.
CThey increase CSS selector complexity.
DThey block rendering until the event finishes.
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for event handler execution time and memory usage.
What to look for: Check if event handlers are quick and if there is excessive memory retained due to event persistence.