Laravel Collections vs Arrays: Performance Test

Laravel Collections vs Arrays: Performance Test

Laravel Daily

2 года назад

25,305 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@usamafiaz530
@usamafiaz530 - 16.08.2022 11:26

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.

Ответить
@MargaRizaldiChannel
@MargaRizaldiChannel - 08.08.2022 06:11

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

Ответить
@thavarajan1
@thavarajan1 - 21.07.2022 06:05

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

Ответить
@nadjinmalade8738
@nadjinmalade8738 - 27.06.2022 21:14

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.

Ответить
@kinvain
@kinvain - 13.06.2022 15:33

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.

Ответить
@kirayamato6128
@kirayamato6128 - 12.06.2022 07:07

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

Ответить
@Niboros
@Niboros - 09.06.2022 16:26

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!

Ответить
@AlexanderWright1
@AlexanderWright1 - 09.06.2022 12:48

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.

Ответить
@AlexanderWright1
@AlexanderWright1 - 09.06.2022 12:46

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

Ответить
@zacharyzheng9326
@zacharyzheng9326 - 09.06.2022 06:37

so the conclusion is array 2x fast than collection?

Ответить
@alila3883
@alila3883 - 08.06.2022 23:13

An excellent example for collections and arrays 👍

Ответить
@TravisFont
@TravisFont - 08.06.2022 12:03

Collection readability is less intuitive.

Ответить
@soniablanche5672
@soniablanche5672 - 08.06.2022 06:39

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 - 08.06.2022 03:19

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!

Ответить
@Juan-hg2ts
@Juan-hg2ts - 08.06.2022 03:12

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

Ответить
@ClCfe
@ClCfe - 07.06.2022 23:41

collect($users)->countBy(function ($user) {
return Str::of($user[1])->before(' ')->toString(); //strstr($user[1],' ', true);
});
->except(['Ben', 'Vesta','Lisa'])
->sortDesc();

Ответить
@jeremyvanderwegen1467
@jeremyvanderwegen1467 - 07.06.2022 22:40

Supplier for the best Laravel tips and tutorials!

Ответить
@tonystark3107
@tonystark3107 - 07.06.2022 21:53

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

Ответить
@RealHomeboy
@RealHomeboy - 07.06.2022 21:38

I hope we once get a typed collection data structure

Ответить
@maflones
@maflones - 07.06.2022 19:42

Do it with a raw SQL query and try again.

Ответить
@ward7576
@ward7576 - 07.06.2022 17:09

Comparing native implementation against a wrapper of native implementation? Native would always be faster.

Ответить
@donmikele07
@donmikele07 - 07.06.2022 17:01

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

Ответить
@underflowexception
@underflowexception - 07.06.2022 15:41

Should of ran it on millions of rows imo

Ответить
@ad-hv3cw
@ad-hv3cw - 07.06.2022 15:40

If you want to have a single run through using collections then I think using lazy collection can achieve that.

Ответить
@Voltra_
@Voltra_ - 07.06.2022 13:56

Would be interesting to compare this with `lazy` collections

Ответить
@olivierperrier114
@olivierperrier114 - 07.06.2022 12:35

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.

Ответить
@obikwelukyrian6848
@obikwelukyrian6848 - 07.06.2022 10:59

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

Ответить
@tetleyk
@tetleyk - 07.06.2022 09:30

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.

Ответить
@alexrusin
@alexrusin - 07.06.2022 09:18

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.

Ответить
@bulent2435
@bulent2435 - 07.06.2022 08:57

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.

Ответить
@1PeZiNhO1
@1PeZiNhO1 - 07.06.2022 08:37

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!

Ответить
@JouvaMoufette
@JouvaMoufette - 07.06.2022 08:31

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

Ответить
@msdeav
@msdeav - 07.06.2022 08:29

thank you...

Ответить
@nazeehsaifi916
@nazeehsaifi916 - 07.06.2022 08:18

Great content 👌

Ответить