Performance: CSS-in-JS patterns with Svelte
MEDIUM IMPACT
This affects page load speed and rendering performance by controlling how styles are injected and updated in the DOM during component lifecycle.
<style>
.dynamic { color: red; }
</style>
<div class="dynamic">Hello</div><script> import { onMount } from 'svelte'; let styleElement; onMount(() => { styleElement = document.createElement('style'); styleElement.textContent = `.dynamic { color: red; }`; document.head.appendChild(styleElement); }); </script> <div class="dynamic">Hello</div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual style injection on mount | Adds style nodes dynamically | 1 reflow per component instance | Moderate paint delay | [X] Bad |
| Runtime CSS-in-JS library usage | Generates styles in JS | 1 reflow on mount | Blocks rendering until JS runs | [X] Bad |
| Svelte scoped styles (compiled) | Static style nodes | No runtime reflows | Fast paint | [OK] Good |