DEV Community

on3ss
on3ss

Posted on

Laravel collection methods vs loops

I just watched Adam Wathan's curing the common loop talk and I can see the advantages of using collections and collection methods over arrays and loops, but surely that would have some performance hit, won't they? What do you guys think? Is using using collections and collection methods over plain old arrays and loops worth it?

Top comments (5)

Collapse
 
lcjury profile image
luis jury senn

It will have a performance hit as you're using functional methods instead of modifying arrays inplace.

Anyway, does the performance hit matters?, I been using Laravel for years, I haven written traditionals-for-loops in a while, and, if any time in the future your app has a performance problem, you can pay 10 extra dollars to add an extra server.

Your development time is way more expensive than some CPU.

If you find collections easier to maintain and test (I do), then it completely worth it.

Collapse
 
futureistaken profile image
R Z

It's fair enough if a website has, for example, 500 users per day. What about a million or more? I am not sure that the 10 dollars you set aside for ice cream will be enough.

Collapse
 
on3ss profile image
on3ss

Tradeoffs as always but i guess if the performance hit isn't so noticeable then maintainable code is a no brainer.

Collapse
 
futureistaken profile image
R Z

It depends. If your code has expensive calculations, use the traditional approach.
As for me, I always try to find a balance between fast and maintainable code.
Anyway, don't save on matches.

Collapse
 
on3ss profile image
on3ss

That sounds like a reasonable thing. I guess there's a tradeoff as always.