0
0
SASSmarkup~8 mins

Grid column generator with loops in SASS - Performance & Optimization

Choose your learning style9 modes available
Performance: Grid column generator with loops
MEDIUM IMPACT
This affects the CSS generation size and browser rendering performance by controlling how many grid column classes are created and used.
Creating reusable grid column classes for layout
SASS
@for $i from 1 through 12 {
  .col-#{$i} {
    grid-column: span #{$i};
  }
}
Limits generated classes to common grid columns, reducing CSS size and improving load speed.
📈 Performance GainSaves ~4-8kb CSS, reduces LCP by faster CSS parsing
Creating reusable grid column classes for layout
SASS
@for $i from 1 through 100 {
  .col-#{$i} {
    grid-column: span #{$i};
  }
}
Generates 100 CSS classes increasing CSS file size and parsing time, even if many are unused.
📉 Performance CostAdds ~5-10kb to CSS bundle, increases LCP due to larger CSS parsing
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Generating 100 grid column classesNo extra DOM nodesNo extra reflowsHigher paint cost due to larger CSS[X] Bad
Generating 12 grid column classesNo extra DOM nodesNo extra reflowsLower paint cost with smaller CSS[OK] Good
Rendering Pipeline
The generated CSS classes are parsed during Style Calculation. More classes increase CSS parsing time and memory. During Layout, grid columns affect element sizing and positioning. Excessive classes increase CSS size but do not increase DOM nodes.
Style Calculation
Layout
Paint
⚠️ BottleneckStyle Calculation due to large CSS size
Core Web Vital Affected
LCP
This affects the CSS generation size and browser rendering performance by controlling how many grid column classes are created and used.
Optimization Tips
1Limit loop ranges to only necessary grid columns to reduce CSS size.
2Avoid generating unused CSS classes to improve load speed.
3Smaller CSS files improve Largest Contentful Paint (LCP) by faster style calculation.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance cost of generating many grid column classes with a loop in Sass?
AMore JavaScript execution time
BMore DOM nodes created in the browser
CIncreased CSS file size causing slower CSS parsing
DMore HTTP requests for each class
DevTools: Performance
How to check: Record page load and look at the 'Style Recalculation' and 'CSS Parsing' times in the flame chart.
What to look for: Long CSS parsing or style recalculation times indicate large CSS impacting LCP.