Performance: Conditional mixins with @if
MEDIUM IMPACT
This affects CSS generation size and browser style recalculation during rendering.
@mixin button-style($type) { color: black; background: white; border: 1px solid gray; @if $type == 'primary' { background: blue; color: white; } @else if $type == 'secondary' { background: gray; } @else if $type == 'danger' { background: red; color: yellow; } } .button-primary { @include button-style('primary'); } .button-secondary { @include button-style('secondary'); } .button-danger { @include button-style('danger'); } .button-default { @include button-style('default'); }
@mixin button-style($type) { color: black; background: white; border: 1px solid gray; @if $type == 'primary' { background: blue; color: white; } @if $type == 'secondary' { background: gray; } @if $type == 'danger' { background: red; color: yellow; } } .button-primary { @include button-style('primary'); } .button-secondary { @include button-style('secondary'); } .button-danger { @include button-style('danger'); } .button-default { @include button-style('default'); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Conditional mixins with multiple @if blocks | No direct DOM impact | Triggers multiple style recalculations | Higher paint cost due to redundant styles | [X] Bad |
| Conditional mixins with @if/@else if chain | No direct DOM impact | Single style recalculation per element | Lower paint cost with minimal styles | [OK] Good |