0
0
Svelteframework~8 mins

Action parameters in Svelte - Performance & Optimization

Choose your learning style9 modes available
Performance: Action parameters
MEDIUM IMPACT
This affects how efficiently event handlers and DOM manipulations run during user interactions.
Passing parameters to a Svelte action for DOM event handling
Svelte
function myAction(node, param) {
  node.textContent = param;
  return {
    update(newParam) {
      if (newParam !== param) {
        param = newParam;
        node.textContent = param;
      }
    }
  };
}

<div use:myAction="hello"></div>
This pattern updates the DOM only when the parameter changes, reducing unnecessary DOM writes and reflows.
📈 Performance Gainreduces reflows to only when parameter changes
Passing parameters to a Svelte action for DOM event handling
Svelte
function myAction(node) {
  return {
    update(param) {
      node.textContent = param;
    }
  };
}

<div use:myAction="hello"></div>
This pattern updates the DOM on every parameter change without checking if the parameter actually changed, causing unnecessary updates and potential reflows.
📉 Performance Costtriggers 1 reflow per parameter update
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Update DOM on every param change (no check)Multiple DOM writesTriggers reflow each updateHigher paint cost[X] Bad
Update DOM only on param changeMinimal DOM writesReflow only when neededLower paint cost[OK] Good
Rendering Pipeline
Action parameters affect the update phase where DOM nodes are manipulated. Efficient parameter handling minimizes layout recalculations and repaints.
Update
Layout
Paint
⚠️ BottleneckLayout (reflows caused by DOM updates)
Core Web Vital Affected
INP
This affects how efficiently event handlers and DOM manipulations run during user interactions.
Optimization Tips
1Only update the DOM inside actions when parameters actually change.
2Avoid recreating action objects unnecessarily to reduce runtime overhead.
3Use browser DevTools Performance panel to monitor layout thrashing caused by action updates.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of checking parameter changes inside a Svelte action before updating the DOM?
AIt reduces unnecessary DOM writes and reflows.
BIt increases the number of event listeners.
CIt delays the initial rendering of the component.
DIt adds more JavaScript to the bundle size.
DevTools: Performance
How to check: Record a performance profile while interacting with the component using action parameters. Look for frequent Layout events during parameter updates.
What to look for: Fewer Layout and Recalculate Style events indicate better performance with action parameters.