When writing loops, optimize for both readability and performance by following these key principles: 1. **Exit early** when a decision can be made: ```php
When writing loops, optimize for both readability and performance by following these key principles:
// Do this: foreach ($keys as $key) { if (! static::has($array, $key)) { return false; } } return true;
2. **Move invariant operations outside loops**:
```php
// Instead of this:
foreach ($array as $key => $item) {
$groupKey = is_callable($groupBy) ? $groupBy($item, $key) : static::get($item, $groupBy);
// ...
}
// Do this:
$groupBy = is_callable($groupBy) ? $groupBy : fn ($item) => static::get($item, $groupBy);
foreach ($array as $key => $item) {
$groupKey = $groupBy($item, $key);
// ...
}
// Do this: if (isset($uses[InteractsWithQueue::class], $uses[Queueable::class])) { // … }
4. **Add early termination conditions** for algorithms that can complete before processing all elements:
```php
// Add early exit:
foreach ($map as $roman => $value) {
while ($number >= $value) {
$result .= $roman;
$number -= $value;
}
if ($number === 0) {
return $result;
}
}
These optimizations help reduce computational complexity and unnecessary operations, resulting in more efficient and maintainable code.
Enter the URL of a public GitHub repository