Performance: Functions vs mixins comparison
This affects CSS generation size and browser rendering speed by controlling how styles are reused and output.
$shadow: 0 4px 6px rgba(0,0,0,0.1); .button { box-shadow: $shadow; } .card { box-shadow: $shadow; }
@mixin box-shadow() { box-shadow: 0 4px 6px rgba(0,0,0,0.1); } .button { @include box-shadow(); } .card { @include box-shadow(); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using mixins for repeated styles | No direct DOM impact | No direct reflows but larger CSS triggers slower style calculation | Higher paint cost due to larger CSS | [X] Bad |
| Using functions for value reuse | No direct DOM impact | No reflows triggered | Lower paint cost with smaller CSS | [OK] Good |
function and a mixin in Sass?double that returns twice the input number?@function keyword and must include a @return statement.@return $n * 2;. Choices using @mixin are incorrect for functions. @function double($n) { $n * 2; } lacks @return.@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}
.button {
@include border-radius(10px);
}.button selector with argument 10px, so all three properties will appear with that value.@function add($a, $b) {
$sum: $a + $b;
}
.result {
width: add(10px, 5px);
}@return.width. Without @return, it returns nothing.Pass the test and this pattern moves unknown