Performance: Null value behavior
How Sass handles null values affects CSS output size and conditional logic efficiency, impacting CSS bundle size and rendering speed.
$color: null; .selector { @if $color { color: $color; } }
$color: null; .selector { color: if($color != null, $color, black); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Output CSS with unnecessary properties due to null checks | N/A | N/A | Higher due to larger CSS | [X] Bad |
| Conditional Sass directives to skip null values | N/A | N/A | Lower due to smaller CSS | [OK] Good |
null represent in Sass?null means no value is assigned, like an empty box.null means nothing is there.$color is not null?!= to check if a value is not equal to another.@if $color != null { ... } is valid. = is assignment, not comparison. !== is not valid Sass syntax.$bg: null;
.box {
background-color: $bg;
color: black;
}null, Sass omits that property from output.background-color: $bg; is omitted because $bg is null. color: black; remains.$padding: null;
.container {
padding: $padding;
@if $padding != null {
border: 1px solid black;
}
}padding: $padding; tries to output padding with null, which Sass omits from CSS.@if $padding != null is false because $padding is null, so border is not added.$margin: null;
$padding: 1rem;
.box {
margin: $margin;
padding: $padding;
@if $margin != null {
border: 1px solid black;
}
@if $padding != null {
background-color: lightgray;
}
}margin: $margin; is null, so omitted. padding: 1rem; outputs normally.@if $margin != null is false, so border is skipped. @if $padding != null is true, so background-color outputs.Pass the test and this pattern moves unknown