Performance: Control structures (@if, @foreach, @for)
MEDIUM IMPACT
These control structures affect how many DOM elements are rendered and how often the template engine processes loops and conditions, impacting page load and rendering speed.
@foreach($items->filter(fn($item) => $item->isActive()) as $item) <li>{{ $item->name }}</li> @endforeach
@foreach($items as $item)
@if($item->isActive())
<li>{{ $item->name }}</li>
@endif
@endforeach| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| @if inside @foreach loop | N condition checks, up to N DOM nodes | Up to N reflows if nodes inserted | Medium paint cost | [!] OK |
| Pre-filter collection before @foreach | Only active items processed | Fewer reflows proportional to filtered count | Lower paint cost | [OK] Good |
| Calling count() in loop condition | Repeated function calls each iteration | No direct reflows but CPU overhead | Low paint cost | [X] Bad |
| Store count() in variable before loop | Single function call per count | No extra reflows | Low paint cost | [OK] Good |