0
0
SASSmarkup~8 mins

Breakpoint variables and maps in SASS - Performance & Optimization

Choose your learning style9 modes available
Performance: Breakpoint variables and maps
MEDIUM IMPACT
This affects CSS file size and browser rendering speed by organizing media queries efficiently.
Defining responsive breakpoints in CSS
SASS
$breakpoints: (
  small: 480px,
  medium: 768px,
  large: 1024px
);

.element {
  @each $name, $size in $breakpoints {
    @media (min-width: $size) {
      font-size: if($name == small, 1rem, if($name == medium, 1.2rem, 1.5rem));
    }
  }
}
Using a map and loop reduces repeated code and CSS output size.
📈 Performance GainSaves CSS bytes and improves LCP by reducing style recalculations.
Defining responsive breakpoints in CSS
SASS
$small: 480px;
$medium: 768px;
$large: 1024px;

.element {
  @media (min-width: $small) { font-size: 1rem; }
  @media (min-width: $medium) { font-size: 1.2rem; }
  @media (min-width: $large) { font-size: 1.5rem; }
}
Repeated media queries with separate variables cause duplicated CSS and larger file size.
📉 Performance CostAdds unnecessary CSS bytes and increases LCP by delaying style application.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Separate variables with repeated media queriesNo extra DOM nodesMultiple reflows if CSS is largeHigher paint cost due to style recalculation[X] Bad
Breakpoint map with looped media queriesNo extra DOM nodesSingle reflow with minimal CSSLower paint cost due to concise styles[OK] Good
Rendering Pipeline
Breakpoint variables and maps help generate concise CSS, reducing the style calculation and layout stages in the browser.
Style Calculation
Layout
Paint
⚠️ BottleneckStyle Calculation due to large or duplicated CSS rules
Core Web Vital Affected
LCP
This affects CSS file size and browser rendering speed by organizing media queries efficiently.
Optimization Tips
1Use breakpoint maps to avoid repeating media queries.
2Loop through breakpoint maps to generate concise CSS.
3Smaller CSS files improve Largest Contentful Paint (LCP).
Performance Quiz - 3 Questions
Test your performance knowledge
Why is using a breakpoint map in Sass better for performance than separate variables?
AIt adds more CSS for better clarity
BIt increases DOM nodes for responsiveness
CIt reduces repeated CSS and file size
DIt delays style application intentionally
DevTools: Performance
How to check: Record a page load and look at the 'Style Recalculation' and 'Layout' timings to see if media queries cause delays.
What to look for: Lower style recalculation and layout times indicate efficient breakpoint usage.