Performance: Why advanced mixins solve complex problems
MEDIUM IMPACT
This affects CSS generation size and browser rendering speed by reducing repetitive code and complex selector duplication.
@mixin button-style($bg, $hover-bg) { background-color: $bg; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.2); &:hover { background-color: $hover-bg; } } .button-primary { @include button-style(blue, darkblue); } .button-secondary { @include button-style(gray, darkgray); }
@mixin button-style { background-color: blue; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.2); &:hover { background-color: darkblue; } } .button-primary { @include button-style; } .button-secondary { background-color: gray; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.2); &:hover { background-color: darkgray; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual repeated styles | No extra DOM nodes | Multiple reflows if styles change | Higher paint cost due to larger CSS | [X] Bad |
| Advanced parameterized mixins | No extra DOM nodes | Single reflow per style change | Lower paint cost due to smaller CSS | [OK] Good |