Performance: @for loop (through vs to)
This affects CSS preprocessing time and the size of the generated CSS file, impacting page load speed.
@for $i from 1 to 10 { .item-#{$i} { width: 10px * $i; } }
@for $i from 1 through 10 { .item-#{$i} { width: 10px * $i; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| @for ... through (1 through 10) | 0 (CSS only) | 0 | Low | [!] OK |
| @for ... to (1 to 10) | 0 (CSS only) | 0 | Lower | [OK] Good |
@for $i from 1 through 5 and @for $i from 1 to 5 in Sass?through keywordthrough keyword means the loop will include the last number in the range.to keywordto keyword means the loop stops before the last number, excluding it.through includes 5, to stops before 5 -> Option Athrough includes last number, to excludes last number [OK]to includes the last number@for syntax@for $i from start to end or through.to, so @for $i from 1 to 5 { } is correct.to to exclude last number [OK]@for $i from 1 through 3 {
.item-#{$i} {
width: 10px * $i;
}
}through@for $i from 1 to 4 {
.box-#{$i} {
height: 5px * $i;
}
}
@for $j from 1 through 4
.circle-#{$j} {
width: 5px * $j;
}.btn-1 to .btn-4 using a Sass @for loop. Which loop should you use to include the last button class .btn-4?through includes the last number, so @for $i from 1 through 4 is correct.through to include last number [OK]Pass the test and this pattern moves unknown