0
0
SASSmarkup~8 mins

List values and operations in SASS - Performance & Optimization

Choose your learning style9 modes available
Performance: List values and operations
MEDIUM IMPACT
This affects CSS preprocessing speed and the size of the generated CSS, impacting initial page load and style calculation.
Managing multiple CSS values using Sass lists
SASS
$colors: red, blue, green, yellow, orange, purple, black, white;

$color1: nth($colors, 1);
$color2: nth($colors, 2);
$color3: nth($colors, 3);
$color4: nth($colors, 4);
$color5: nth($colors, 5);
$color6: nth($colors, 6);
$color7: nth($colors, 7);
$color8: nth($colors, 8);

.element {
  color: $color1;
  background-color: $color2;
  border-color: $color3;
  box-shadow: 0 0 5px $color4;
  outline-color: $color5;
  fill: $color6;
  stroke: $color7;
  caret-color: $color8;
}
Extracting list values once into variables avoids repeated list traversal, reducing compile time.
📈 Performance GainSingle list traversal per value extraction, reducing compile time by up to 8x for 8 values.
Managing multiple CSS values using Sass lists
SASS
$colors: red, blue, green, yellow, orange, purple, black, white;

.element {
  color: nth($colors, 1);
  background-color: nth($colors, 2);
  border-color: nth($colors, 3);
  box-shadow: 0 0 5px nth($colors, 4);
  outline-color: nth($colors, 5);
  fill: nth($colors, 6);
  stroke: nth($colors, 7);
  caret-color: nth($colors, 8);
}
Repeatedly calling nth() on a long list causes Sass to traverse the list multiple times, slowing compilation.
📉 Performance CostTriggers multiple list traversals, increasing Sass compile time linearly with list length.
Performance Comparison
PatternSass Compile TimeCSS SizeBrowser Style CalculationVerdict
Repeated nth() calls on long listHigh (multiple traversals)Medium (same CSS size)Medium (normal CSS complexity)[X] Bad
Extract list values once to variablesLow (single traversal per value)Medium (same CSS size)Medium (normal CSS complexity)[OK] Good
Rendering Pipeline
Sass list operations happen during preprocessing before the browser rendering pipeline. Efficient list handling reduces CSS file size and complexity, which speeds up style calculation and paint in the browser.
Preprocessing
Style Calculation
Paint
⚠️ BottleneckPreprocessing time increases with inefficient list operations; large CSS slows Style Calculation.
Core Web Vital Affected
LCP
This affects CSS preprocessing speed and the size of the generated CSS, impacting initial page load and style calculation.
Optimization Tips
1Avoid calling nth() repeatedly on the same Sass list; extract values once.
2Keep Sass lists concise to reduce compile time and CSS size.
3Smaller, simpler generated CSS improves browser style calculation and LCP.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue with calling nth() repeatedly on a long Sass list?
AEach nth() call traverses the list, increasing compile time linearly.
Bnth() calls increase CSS file size significantly.
Cnth() causes browser to repaint multiple times.
Dnth() slows down JavaScript execution.
DevTools: Performance
How to check: Record Sass compilation time in your build tool or IDE; in browser DevTools, check CSS file size and style recalculation time under Performance tab.
What to look for: Look for long Sass compile times or large CSS files causing slow style recalculation and paint.