Performance: sass:map module
Using the sass:map module affects the CSS compilation speed and the size of the generated CSS, impacting initial page load speed.
$colors: (primary: #ff0000, secondary: #00ff00, accent: #0000ff); .button { color: map.get($colors, primary); } .alert { color: map.get($colors, secondary); } .link { color: map.get($colors, accent); }
$colors-primary: #ff0000; $colors-secondary: #00ff00; $colors-accent: #0000ff; .button { color: $colors-primary; } .alert { color: $colors-secondary; } .link { color: $colors-accent; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Separate variables for related values | N/A | N/A | Higher due to larger CSS | [X] Bad |
| Using sass:map module for related values | N/A | N/A | Lower due to smaller CSS | [OK] Good |
sass:map module in Sass?sass:mapsass:map module provides functions to work with maps, which are collections of key-value pairs.$colors for the key primary using the sass:map module?map-getmap-get($map, key) without quotes around the key unless it is a string literal.$colors is the map and primary is the key, so map-get($colors, primary) is correct.$theme-colors: (primary: #0055ff, secondary: #ff5500);
color: map-get($theme-colors, secondary);?$theme-colors has primary: #0055ff and secondary: #ff5500.secondarymap-get($theme-colors, secondary) returns #ff5500.$settings: (font-size: 16px, color: blue);
$new-settings: map-merge($settings color: red);
map-merge syntaxmap-merge($map, (key: value)) with a comma separating arguments and the new pairs inside parentheses.$settings and the new pair color: red, and the new pair is not wrapped in parentheses.$sizes: (small: 1rem, medium: 2rem, large: 3rem);
$updated-sizes that changes medium to 2.5rem but keeps other sizes unchanged using sass:map functions?map-merge can merge the original map with a new map containing the updated key-value pair.map-merge to update mediummap-merge($sizes, (medium: 2.5rem)) replaces the medium value while keeping others unchanged.Pass the test and this pattern moves unknown