Performance: Lifecycle in nested components
MEDIUM IMPACT
This affects the initial page load speed and interaction responsiveness by controlling when nested components run their setup and teardown code.
/* In a nested Svelte component */ onMount(() => { // Defer heavy work asynchronously setTimeout(() => { // heavy task runs after initial render }, 0); });
/* In a nested Svelte component */ onMount(() => { // Heavy synchronous task like large data processing for (let i = 0; i < 1_000_000; i++) { // simulate work } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous work in nested onMount | Multiple nested nodes mounted | Multiple reflows if layout changes | High paint cost due to delayed rendering | [X] Bad |
| Deferred heavy work after mount | Same DOM operations | Single reflow during mount | Lower paint cost, faster interaction | [OK] Good |