0
0
SASSmarkup~8 mins

Mixins with parameters in SASS - Performance & Optimization

Choose your learning style9 modes available
Performance: Mixins with parameters
MEDIUM IMPACT
This affects CSS generation size and browser rendering speed by controlling how styles are reused and customized.
Reusing styles with slight variations
SASS
@mixin button-style($bg-color) {
  padding: 1rem 2rem;
  border-radius: 0.5rem;
  background-color: $bg-color;
  color: white;
}

.button-primary {
  @include button-style(blue);
}

.button-secondary {
  @include button-style(gray);
}
Parameterizing the background color reduces duplication by reusing the same mixin with different inputs.
📈 Performance GainReduces CSS file size, improving load speed and parsing efficiency.
Reusing styles with slight variations
SASS
@mixin button-style {
  padding: 1rem 2rem;
  border-radius: 0.5rem;
  background-color: blue;
  color: white;
}

.button-primary {
  @include button-style;
}

.button-secondary {
  @include button-style;
  background-color: gray;
}
This duplicates the entire mixin output for each variation, increasing CSS size unnecessarily.
📉 Performance CostAdds extra CSS bytes for each variation, increasing download and parse time.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Non-parameterized mixins with many variationsN/AN/AHigher due to larger CSS[X] Bad
Parameterized mixins reusing styles efficientlyN/AN/ALower due to smaller CSS[OK] Good
Rendering Pipeline
Sass mixins with parameters compile to CSS before the browser sees them, affecting the size and complexity of the CSS file the browser loads.
Network Transfer
Style Calculation
Layout
⚠️ BottleneckNetwork Transfer and Style Calculation due to larger CSS files
Core Web Vital Affected
LCP
This affects CSS generation size and browser rendering speed by controlling how styles are reused and customized.
Optimization Tips
1Use parameters in mixins to avoid repeating similar CSS blocks.
2Avoid generating too many unique variations to keep CSS size small.
3Smaller CSS files improve load speed and reduce rendering delays.
Performance Quiz - 3 Questions
Test your performance knowledge
How do parameterized mixins in Sass affect CSS file size?
AThey have no effect on CSS file size.
BThey reduce duplication by reusing styles with different inputs.
CThey always increase CSS file size by duplicating all styles.
DThey remove all CSS styles from the output.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, filter by CSS files, and check file sizes.
What to look for: Look for smaller CSS file sizes indicating less duplication and faster load times.