Laravel Collections vs Arrays: Performance Test

  Рет қаралды 25,436

Laravel Daily

2 жыл бұрын

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:
- My Laravel courses membership: laraveldaily.teachable.com/p/all-courses-yearly-membership?
- Laravel QuickAdminPanel: bit.ly/quickadminpanel
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: bit.ly/laravel-newsletter
- My personal Twitter: povilaskorop

Пікірлер: 59
@alexrusin
@alexrusin 2 жыл бұрын
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.
@tetleyk
@tetleyk 2 жыл бұрын
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.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Agree! I rephrase the same thought as: 1. Make it work 2. Make it maintainable (readable / reusable for others) 3. Make it scalable (performance)
@bulent2435
@bulent2435 2 жыл бұрын
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.
@marcusgaius
@marcusgaius 2 жыл бұрын
It adds up.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I agree with this point a lot. A broader approach of this is not to perform "premature optimization". Of course, it depends on the situation, in some cases you need to take care of the scalable solution from the very beginning, because re-writing would be hugely expensive, but in most cases, you start with MVP and then refactor/improve different parts, like if you diagnose later that collections specifically make some feature slow.
@bulent2435
@bulent2435 2 жыл бұрын
@@LaravelDaily I agree with you.
@MargaRizaldiChannel
@MargaRizaldiChannel 2 жыл бұрын
I think this is also the reason for using eloquent over query builder...
@AlexanderWright1
@AlexanderWright1 2 жыл бұрын
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.
@donmikele07
@donmikele07 2 жыл бұрын
Great explanation! Great comparison! Great experience! Thank you!
@jeremyvanderwegen1467
@jeremyvanderwegen1467 2 жыл бұрын
Supplier for the best Laravel tips and tutorials!
@alila3883
@alila3883 2 жыл бұрын
An excellent example for collections and arrays 👍
@Niboros
@Niboros 2 жыл бұрын
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!
@nazeehsaifi916
@nazeehsaifi916 2 жыл бұрын
Great content 👌
@nadjinmalade8738
@nadjinmalade8738 2 жыл бұрын
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.
@msdeav
@msdeav 2 жыл бұрын
thank you...
@kinvain
@kinvain 2 жыл бұрын
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.
@RealHomeboy
@RealHomeboy 2 жыл бұрын
I hope we once get a typed collection data structure
@Voltra_
@Voltra_ 2 жыл бұрын
Would be interesting to compare this with `lazy` collections
@Niboros
@Niboros 2 жыл бұрын
That would be interesting! For some situations lazy collections might make sence. I do not have enough experience with them to fully know when to use them. Excellent video idea I think.
@ManuelDonini
@ManuelDonini 2 жыл бұрын
Lazy collections are made to use less RAM, not to be faster. Even though I would be interested in a speed comparison, just for curiosity
@Voltra_
@Voltra_ 2 жыл бұрын
AFAIK, lazy collections are based on lazy sequence evaluation principles (using generators) which mean that a sequence will (in most cases) be iterated over only once no matter the amount of operations done to it. So lazy collections should be faster than regular collection who AFAIK store the "intermediate" data in an internal buffer
@olivierperrier114
@olivierperrier114 2 жыл бұрын
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.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Interesting, never tried countBy
@AlexanderWright1
@AlexanderWright1 2 жыл бұрын
I'd be interested in the time an SQL query would take to perform this operation.
@Juan-hg2ts
@Juan-hg2ts 2 жыл бұрын
Great comparison thanks, I'd like to know, what javascript framework is more popular with Laravel, and what is more efficient ????
@LaravelDaily
@LaravelDaily 2 жыл бұрын
It's personal preference about efficiency. In terms of popularity, Vue is by far the most popular among Laravel developers.
@soniablanche5672
@soniablanche5672 2 жыл бұрын
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.
@intipontt7490
@intipontt7490 2 жыл бұрын
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 Illuminate\Support\LazyCollection; 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!
@intipontt7490
@intipontt7490 2 жыл бұрын
Even if you're not using a large csv as the source, LazyCollections are available in the query builder too via the cursor() method.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Yes, planning a video on exactly that, on Lazy Collections, after a few people pointed it as well. Thanks!
@MargaRizaldiChannel
@MargaRizaldiChannel 2 жыл бұрын
Is there any way to measure the memory usage of a collection? I have a killed job issue when operating huge data through collection...
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Yes with Laravel telescope, see my video: kzfaq.info/get/bejne/rap7h6Z_xMmuook.html
@tonystark3107
@tonystark3107 2 жыл бұрын
Hello Sir, can you make a video on web 3.0 and where does php and laravel stands in it and how to create projects using these. or any other info related web 3.0 and php/laravel. Thanks
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I don't work with web 3.0 so can't make a video about that. But a few resources I've seen about it: - github.com/m1guelpf/laravel-web3-login - medium.com/geekculture/laravel-authentication-using-web3-15d0fb030a48 - www.freecodecamp.org/news/add-a-metamask-login-to-your-laravel-app/
@obikwelukyrian6848
@obikwelukyrian6848 2 жыл бұрын
Will the use of Lazy Collections in this case save time or is it just reducing the memory the application uses??
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Aren't those two things directly related?
@BlurSentinel
@BlurSentinel 2 жыл бұрын
It makes no sense to use something Lazy, IF you need to access all data to get the result you want. (Group by, Sort etc.).
@kirayamato6128
@kirayamato6128 2 жыл бұрын
Because the groupby method is an aggregation that's why its is slow. You will notice it if you are working on TSQL with complex queries
@usamafiaz530
@usamafiaz530 Жыл бұрын
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.
@LaravelDaily
@LaravelDaily Жыл бұрын
I don't remember any video about it
@usamafiaz530
@usamafiaz530 Жыл бұрын
@@LaravelDaily Model::cursor(); something like this?
@LaravelDaily
@LaravelDaily Жыл бұрын
Yes so I don't remember shooting videos about it
@maflones
@maflones 2 жыл бұрын
Do it with a raw SQL query and try again.
@zacharyzheng9326
@zacharyzheng9326 2 жыл бұрын
so the conclusion is array 2x fast than collection?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
No, you probably haven't watched the full video with my thoughts. The conclusion is "fast but it depends".
@zakariazakaria2749
@zakariazakaria2749 2 жыл бұрын
Please can we get your laravel live wire video Here on KZfaq
@ad-hv3cw
@ad-hv3cw 2 жыл бұрын
If you want to have a single run through using collections then I think using lazy collection can achieve that.
@1PeZiNhO1
@1PeZiNhO1 2 жыл бұрын
I don't think it's a fair comparison. In the first example, only one loop is made, that is: 100 000 lines. In the second, there are 2 maps, a filter and a group, totaling 400 000 lines. As stated in the video, to really draw a conclusion, it is necessary to compare collection methods similar to the ones you will use. Nice video!
@tetleyk
@tetleyk 2 жыл бұрын
It is perfectly fair. Both methods start with a dataset and output a result. One method being faster than the other because of the multiple loops over the dataset. Is that difference significant? Nope, not really. Both results are sub one second. If the difference had been greater, say ten seconds, then the difference would be significant and you'd choose the faster one or find another way to use collections faster. This is exactly the type of thing you will find in your code, anyone's code, and the trick is to find out where the significant bottlenecks are and why they occur and concentrate the development in those areas to speed up the process.
@1PeZiNhO1
@1PeZiNhO1 2 жыл бұрын
@@tetleyk The purpose of the video is to compare performance between two things: arrays and collections. So for it to be a FAIR comparison, equal methods must be used. So for that, you should have used, in the sequence, in the first example: array_map, array_filter and two other custom methods to do the groupBy and mapWithKeys. There's no way to be a fair comparison of performance when each example was done in a different way. At no time was it said in the video that it was for you to learn to identify performance problems, but a comparison between two things was shown: arrays and collections
@marcusgaius
@marcusgaius 2 жыл бұрын
​@@1PeZiNhO1 No, that is not what the purpose was, and foreach is a valid array method. Keyword, rather, but it's still valid. You are forced to chain map and filter to collections, but you're not forced to do that with arrays.
@underflowexception
@underflowexception 2 жыл бұрын
Should of ran it on millions of rows imo
@ward7576
@ward7576 2 жыл бұрын
Comparing native implementation against a wrapper of native implementation? Native would always be faster.
@thavarajan1
@thavarajan1 2 жыл бұрын
I think you're comparing oranges to apples, please use other method like countby or some other method, why we need a group by here
@JouvaMoufette
@JouvaMoufette 2 жыл бұрын
There is almost never a need to use array_key_exists unless you are testing for existence of a key AND want to get a return value of true if the value itself is null. If you know values won't be null OR you wish to ignore them, isset is MUCH faster, especially at this scale of number of records
@TravisFont
@TravisFont 2 жыл бұрын
Collection readability is less intuitive.
@ClCfe
@ClCfe 2 жыл бұрын
collect($users)->countBy(function ($user) { return Str::of($user[1])->before(' ')->toString(); //strstr($user[1],' ', true); }); ->except(['Ben', 'Vesta','Lisa']) ->sortDesc();
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Thanks, great improvements! I've never used countBy() before, will try it out and probably tweet to others.
Каха заблудился в горах
00:57
К-Media
Рет қаралды 9 МЛН
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 11 МЛН
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Паша Осадчий
Рет қаралды 7 МЛН
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 16 МЛН
路飞太过分了,自己游泳。#海贼王#路飞
0:28
路飞与唐舞桐
Рет қаралды 33 МЛН
WORLD'S SHORTEST WOMAN
0:58
Stokes Twins
Рет қаралды 68 МЛН
The master set up a pillar among the people in seconds
0:17
Dice Master_1910
Рет қаралды 73 МЛН