Laravel Collections vs Arrays: Performance Test

preview_player
Показать описание
People say that Laravel Collections operations are slower than arrays. But by how much? Is the difference significant? Let me show you an example and share my thoughts.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

I think it was Steve McConnell that said that it is important to "make the code work, then make it fast". Until it works and you test and measure it you don't know which parts are making your application slow. This is an example of what he was saying.

The more experienced you are the more you know what code constructs are faster than others but the real test is to measure it.

But only when the application works.

tetleyk
Автор

I wish we analyzed time complexities of each solution before running the test. Anyway, if you want effectively use Laravel collections, it is a good idea to know time complexities of the methods.

alexrusin
Автор

IMO, developer time is more important than the run-time. Since we have very fast connections and very strong hardwares, slighty slower code won't effect user experiance negatively. If a table becomes huge and starts slowing down the whole system, it will always be an option to recreate its queries and codes to improve the performance. So my motto "use collections until you shouldn't." Spending your time today for a future that might never come is not a good idea.

bulent
Автор

I've not done benchmarks, but I do recognise the time saved debugging and understanding code that I've come back to after a significant time period as a good reason to use collections, as opposed to loops as in the first example.

AlexanderWright
Автор

I like this video.
Once we hade perfommance issue with data-transformation, and my Lead Dev, ask me to write DB query; instead of using Eloquent Collection.
For 100.000 rows On DB, the differences was Huge.
It takes more time for devs, but for huge amount of data It is always better to write Optimized code on the begining, instant of rewriting when the dB get bigger.

nadjinmalade
Автор

I think the main power of Collection is the sugar they add. Yes, they could slow your code to operate. But coding is not only execute the code. It's also about writing and maintaining your code. So, the true power of Collection is that you code faster.

kinvain
Автор

I am also interested to know what is easier to write and to read. Maybe there are situations where the execution time is not that important, but the time to develop the code is. First make things work, later make it faster when needed. That being said, I really like this topic because I feel it is important to make informed decisions on stuff like this. Thanks for the video!

Niboros
Автор

Supplier for the best Laravel tips and tutorials!

jeremyvanderwegen
Автор

Great video. Could you use countBy collection method rather than groupBy and mapWithKeys ?
This is specific to this use case but here the groupBy is doing not necessary calculation.

olivierperrier
Автор

Great explanation! Great comparison! Great experience! Thank you!

donmikele
Автор

Great comparison thanks, I'd like to know, what javascript framework is more popular with Laravel, and what is more efficient ????

Juan-hgts
Автор

I'd be interested in the time an SQL query would take to perform this operation.

AlexanderWright
Автор

Would be interesting to compare this with `lazy` collections

Voltra_
Автор

In this particular case, I believe you could make better use of Lazy Collection (included in the framework since laravel 6). One of the examples from the original PR was precisely about dealing with these kinds of large CSV files.

Some methods are not available to LazyCollections however, like mapWithKeys, but I think you can get the exact same result by changing with `->map->count()`.

use

LazyCollection::make(
function () {
$handle = fopen(public_path('users.csv', 'r'));
while( ($line = fgets($handle)) !== false ) {
yield $line;
}
})
->map(...)
->filter(...)
->groupBy(...)
->map->count()
->sortDesc();

Try it out!

intipontt
Автор

An excellent example for collections and arrays 👍

alila
Автор

Is there any way to measure the memory usage of a collection? I have a killed job issue when operating huge data through collection...

MargaRizaldiChannel
Автор

Will the use of Lazy Collections in this case save time or is it just reducing the memory the application uses??

obikwelukyrian
Автор

so the conclusion is array 2x fast than collection?

zacharyzheng
Автор

sir kindly can you share the link to the video in which you teach about cursor() loading, I think right after this but unfortunately, I didn't find it in your list.

usamafiaz
Автор

I haven't read the code of Collection but most of them should be wrappers for php native methods so you shouldn't see too much performance difference.

soniablanche