visit
PHP
array_filter($data, function($row) {
return substr($row, 0, 1) === "a";
});
Collections
$data->filter(function($row) {
return substr($row, 0, 1) === "a";
});
PHP
array_search(function($row) {
return substr($row, 0, 1) === "a";
}, $data);
Collections
$data->search(function($row) {
return substr($row, 0, 1) === "a";
});
PHP
array_map(function($row) {
return "test";
}, $data);
Collections
$data->map(function($row) {
return "test";
});
PHP
sort($data);
Collections
$data->sort();
PHP
foreach($loop as $item) {
$doSomething = true;
}
Collections
$data->each(function($row) {
return "test";
});
PHP
array_reduce($data, function($carry, $row) {
return $carry + strlen($row);
});
Collections
$data->reduce(function($carry, $row) {
return $carry + strlen($row);
});
PHP
array_splice($data, count($data)/2);
Collections
$data->splice(count($data)/2);
All Together (PHP)
$data = array_filter($data, function($row) {
return substr($row, 0, 1) === "a";
});
$data = array_search(function($row) {
return substr($row, 0, 1) === "a";
}, $data);
$data = array_map(function($row) {
return "test";
}, $data);
sort($data);
foreach($loop as $item) {
$doSomething = true;
}
$sum = array_reduce($data, function($carry, $row) {
return $carry + strlen($row);
});
All Together (Collections)
$sum = $data->filter(function($row) {
return substr($row, 0, 1) === "a";
})->search(function($row) {
return substr($row, 0, 1) === "a";
})->map(function($row) {
return "test";
})->sort()
->each(function($row) {
return "test";
})->reduce(function($carry, $row) {
return $carry + strlen($row);
});
Comparison
With an approach this simple, there doesn’t seem to be a huge tradeoff in readability for each individual function, although when you consider the example where you need them all to be applied to a single array, you can clearly see that it is more concise and easier to read when using the chained-methods in a collection.
Each array had 100,000 random strings as items, and I ran each function 100 times. In the end, we calculated the average of all of the response times.
========== Tests Complete (ms) ==========
php filter: 5.07
collect filter: 6.49
=======================
php search: 0.79
collect search: 0
=======================
php map: 3.45
collect map: 4.18
=======================
php sort: 25.27
collect sort: 11.18
=======================
php each: 1.03
collect each: 6.96
=======================
php reduce: 2.78
collect reduce: 7.75
=======================
php splice: 1
collect splice: 0.74
=======================
Filter, Map, Foreach, and Reduce are all faster with the standard PHP functions. Foreach and Reduce are actually incredibly significant differences. Search, Sort, and Splice all show Collections as the winner, and Sort is actually a massive time saver.
In my opinion (and it is just an opinion based on these results), if performance is a huge concern, I would stick with standard PHP functionality for Foreach loops for sure and likely the same for any Reduce needs. If you need to do any sorting on large datasets, clearly Collections is the right way to go. The rest are so close that it really feels like a personal preference.
Obviously, you should take this information and make your own informed decision, however, if you’re like me, I think you’ll find yourself slipping collections in for many of these functions above. But I do think I’ll dial back my use of →each
and →reduce
where appropriate going forward!