A Laravel/PHP Package for Minimizing Collection/Array Iterations
hassan/one-loop is a Laravel package for a laravel/php package for minimizing collection/array iterations.
It currently has 13 GitHub stars and 6 downloads on Packagist (latest version 2.0.0).
Install it with composer require hassan/one-loop.
Discover more Laravel packages by hassan
or browse all Laravel packages to compare alternatives.
Last updated
A Laravel/PHP Package for Minimizing Collection/Array Iterations - optimized for large datasets (100,000+ records).
Real-world performance tests show significant improvements with large datasets:
| Dataset Size | Operations | Standard PHP | OneLoop | Improvement | |-------------|-----------|--------------|---------|-------------| | 500,000 | Complex (3+ ops) | 163.03 ms | 105.84 ms | 35% faster โ | | 500,000 | Simple (2 ops) | 226.32 ms | 161.79 ms | 29% faster โ | | 100,000 | Complex (3+ ops) | 25.83 ms | 18.54 ms | 28% faster โ | | 10,000 | Any | 0.98 ms | 1.84 ms | 88% slower โ ๏ธ | | 1,000 | Any | 0.08 ms | 0.19 ms | 137% slower โ ๏ธ |
This package is optimized for large datasets. It provides significant performance improvements when:
For small datasets (< 50,000 records), standard PHP array functions or Laravel Collections will be faster due to lower overhead.
Install the package via composer:
composer require hassan/one-loop
$users = App\User::all();
$ids = one_loop($users)->reject(static function ($user) {
return $user->age < 20;
})
->map(static function ($user) {
return $user->id;
})
->apply();
limit() / take()Stop processing once you have enough results:
// Get first 100 active users
$users = one_loop($allUsers)
->filter(fn($user) => $user->active)
->limit(100)
->apply();
// Alias: take()
$users = one_loop($allUsers)
->filter(fn($user) => $user->active)
->take(100)
->apply();
pluck()// Pluck by property name
$emails = one_loop($users)
->pluck('email')
->apply();
// Pluck with callback
$fullNames = one_loop($users)
->pluck(fn($user) => $user->first_name . ' ' . $user->last_name)
->apply();
unique()// Unique values
$uniqueDepartments = one_loop($employees)
->pluck('department')
->unique()
->apply();
// Unique by key
$uniqueUsers = one_loop($users)
->unique('email')
->apply();
groupBy()// Group by property
$byDepartment = one_loop($employees)
->groupBy('department')
->apply();
// Group by callback
$byAgeGroup = one_loop($users)
->groupBy(function($user) {
if ($user->age < 30) return 'young';
if ($user->age < 50) return 'middle';
return 'senior';
})
->apply();
when()$shouldFilterActive = true;
$result = one_loop($users)
->when($shouldFilterActive, function($loop) {
$loop->filter(fn($user) => $user->active);
})
->map(fn($user) => $user->id)
->apply();
OneLoop automatically integrates with Laravel Collections:
use Illuminate\Support\Collection;
// Use on any Collection
$result = User::all()
->oneLoop()
->filter(fn($user) => $user->active)
->map(fn($user) => $user->id)
->apply();
filter(callable $callback) - Keep items that match conditionreject(callable $callback) - Remove items that match conditionmap(callable $callback) - Transform each itempluck(string|callable $value) - Extract specific propertyunique(?string|callable $key = null) - Remove duplicatesgroupBy(string|callable $groupBy) - Group by key or callbacklimit(int $limit) - Limit results (early exit)take(int $count) - Alias for limit()when(bool $condition, callable $callback, ?callable $default = null) - Conditional operationsapply() - Execute all queued operations and return results// E-commerce: Process large product catalog
$products = Product::all() // 500,000 products
->oneLoop()
->filter(fn($p) => $p->active)
->reject(fn($p) => $p->stock <= 0)
->when($categoryFilter, fn($loop) =>
$loop->filter(fn($p) => in_array($p->category_id, $categoryFilter))
)
->map(fn($p) => [
'id' => $p->id,
'name' => $p->name,
'price' => $p->price * 0.9 // 10% discount
])
->limit(1000)
->apply();
// Result: 35% faster than standard operations!
composer test
Please see CHANGELOG for more information on what has changed recently.
Contributions are welcome! Please feel free to submit a Pull Request.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.