0
0
Svelteframework~8 mins

Event forwarding in Svelte - Performance & Optimization

Choose your learning style9 modes available
Performance: Event forwarding
MEDIUM IMPACT
Event forwarding affects interaction responsiveness and event handling efficiency in the browser.
Forwarding events from a child component to a parent component
Svelte
/* Child.svelte */
<button on:click>Click me</button>

/* Parent.svelte */
<Child on:click={handleChildClick} />
Using Svelte's native event forwarding by omitting manual dispatch reduces event listeners and speeds up event propagation.
📈 Performance GainSingle event listener per event, reducing event processing overhead by 50%.
Forwarding events from a child component to a parent component
Svelte
/* Child.svelte */
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();

  function handleClick(event) {
    dispatch('click', event);
  }
</script>
<button on:click={handleClick}>Click me</button>

/* Parent.svelte */
<Child on:click={handleChildClick} />
Manually forwarding events creates an extra event dispatch and listener, adding overhead and delaying event handling.
📉 Performance CostTriggers 2 event listeners per click instead of 1, increasing event processing time.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Manual event dispatch forwardingAdds extra event listeners00[X] Bad
Native event forwarding (Svelte default)No extra listeners00[OK] Good
Rendering Pipeline
Event forwarding affects the event handling stage where events propagate from child to parent components. Extra dispatch calls add to the event queue and increase processing time.
Event Handling
JavaScript Execution
⚠️ BottleneckExtra event dispatch and listener calls increase JavaScript execution time.
Core Web Vital Affected
INP
Event forwarding affects interaction responsiveness and event handling efficiency in the browser.
Optimization Tips
1Avoid manually dispatching events if native forwarding suffices.
2Minimize the number of event listeners to improve input responsiveness.
3Use browser DevTools Performance panel to identify event handling bottlenecks.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance drawback of manually forwarding events in Svelte?
AIt adds extra event listeners and dispatch calls, increasing event processing time.
BIt causes layout shifts during event handling.
CIt increases the initial bundle size significantly.
DIt blocks rendering until the event is handled.
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for event handler call stacks and JavaScript execution time related to event dispatch.
What to look for: Multiple event listener calls for the same event indicate manual forwarding overhead. Single listener calls indicate efficient forwarding.