Performance: Maps for grouped values
Using maps for grouped values affects CSS generation size and style recalculation speed in the browser.
$theme: ( colors: ( primary: #0055ff, secondary: #ffaa00 ), font-sizes: ( small: 0.8rem, large: 1.5rem ) ); .button-primary { color: map-get(map-get($theme, colors), primary); font-size: map-get(map-get($theme, font-sizes), large); } .button-secondary { color: map-get(map-get($theme, colors), secondary); font-size: map-get(map-get($theme, font-sizes), small); }
$primary-color: #0055ff; $secondary-color: #ffaa00; $font-size-small: 0.8rem; $font-size-large: 1.5rem; .button-primary { color: $primary-color; font-size: $font-size-large; } .button-secondary { color: $secondary-color; font-size: $font-size-small; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using individual variables repeatedly | N/A | N/A | Higher due to larger CSS | [X] Bad |
| Using grouped maps for values | N/A | N/A | Lower due to smaller CSS | [OK] Good |
maps in Sass?$colors with keys primary and secondary?$theme-colors: (primary: #0055ff, secondary: #ff5500);
color: map.get($theme-colors, primary);?$fonts: (heading: 'Arial', body: 'Helvetica'); $main-font: map.get($fonts, 'heading');
$button-styles that groups background, color, and border-radius for buttons. Which code correctly defines this map and accesses the color value?Pass the test and this pattern moves unknown