Laravel collection methods vs loops
on3ss

on3ss @on3ss

Joined:
Jul 14, 2020

Laravel collection methods vs loops

Publish Date: Sep 28 '20
2 5

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?

Comments 5 total

  • luis jury senn
    luis jury sennSep 28, 2020

    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.

    • on3ss
      on3ssSep 29, 2020

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

    • R Z
      R ZSep 29, 2020

      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.

  • R Z
    R ZSep 28, 2020

    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.

    • on3ss
      on3ssSep 29, 2020

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

Add comment