Performance: Default prop values
This affects initial component rendering speed and bundle size by controlling how props are assigned default values.
export let count = 0;
export let count; if (count === undefined) { count = 0; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Default value assigned inside render logic | No extra DOM nodes | No extra reflows | Minimal paint | [!] OK |
| Default value assigned at prop declaration | No extra DOM nodes | No extra reflows | Minimal paint | [OK] Good |
color in a Svelte component?export let to make them accessible from outside.= 'blue' after export let color sets the default value if no prop is passed.size with a default value of 10 in Svelte?export let prop = value;.size and default 10. <script>
export let message = 'Hello';
</script>
<p>{message}</p>message prop?message has a default value of 'Hello'.<script>
export let count;
if (count === undefined) {
count = 5;
}
</script>
<p>Count is {count}</p>items which should default to an empty array if not provided. Which is the best way to declare this prop to avoid errors when iterating over items in the template?export let items = []; ensures items is always an array, avoiding errors in iteration.Pass the test and this pattern moves unknown