Performance: Built-in math functions
Using built-in math functions in Sass affects the CSS compilation speed and the final CSS size, impacting page load time.
@use "sass:math"; $width: math.add(100px, 50px); $height: math.multiply(200px, 2); .box { width: #{$width}; height: #{$height}; }
$width: 100px + 50px; $height: 200px * 2; .box { width: calc(100px + 50px); height: calc(200px * 2); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using CSS calc() for fixed values | 0 | 1+ at runtime | Medium due to layout recalculation | [X] Bad |
| Using Sass built-in math functions | 0 | 0 at runtime | Low, precomputed values | [OK] Good |
@use rule to import built-in modules like math.@use 'sass:math';. Other options use outdated or incorrect syntax.@use 'sass:math';
$val: math.ceil(4.3);
.result {
width: $val * 1rem;
}math.ceil(4.3) rounds 4.3 up to the nearest whole number, which is 5..result { width: 5rem; }.@use 'sass:math';
$angle: 45deg;
$half: math.div($angle, 2px);
.box {
transform: rotate($half);
}math.min() returns the smaller of two values. math.max() returns the larger, and math.round() rounds numbers, so they are incorrect here.Pass the test and this pattern moves unknown