Performance: When to use SASS vs CSS-in-JS
This affects page load speed and runtime rendering performance by influencing CSS bundle size and style recalculations.
Jump into concepts and practice - no test required
$primary-color: blue; .button { background-color: $primary-color; padding: 1rem; }
@jsx import React from 'react'; const Button = () => { return <button style={{ backgroundColor: 'blue', padding: '10px' }}>Click</button>; };
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| SASS static CSS | Minimal - styles precompiled | 0 on load | Low - browser optimized | [OK] Good |
| CSS-in-JS for static styles | Extra JS nodes for styles | Multiple on load | Medium - runtime style injection | [X] Bad |
| CSS-in-JS for dynamic styles | Scoped style nodes | Few on interaction | Low to medium - conditional styles | [!] OK |
SASS instead of CSS-in-JS?@import 'filename.scss'; to include other SASS files.@import 'variables.scss'; which is correct. @import 'styles.css'; imports a CSS file, which is allowed but not typical for SASS partials. Options C and D use JavaScript syntax, which is incorrect in SASS.$primary-color: blue;
.button {
color: $primary-color;
&:hover {
color: darken($primary-color, 20%);
}
}$primary-color is set to blue and used as the button text color.darken($primary-color, 20%) function makes the blue color 20% darker on hover.const styles = {
button: {
color: 'blue',
'&:hover': {
color: 'darkblue'
}
}
};