1 month ago

Refactoring for speed. Foreach vs Maps

Posted 1 month ago by RafaelMunoznl

I am almost ready building a Web app for a small company that keeps track of appointments from each employee in hourly and daily basis. It means 1 employee x 8hour = 10 slots/day. Each slot has a bunch of data associated. (productivity, prices, clients and so on).

I have to recurrently map collections to each other. Collections like: companyOpeningtime, employeeWorkingTime, publicHolidays, workablesDays, employees, clientsHistory, ClientsPaymentMethods. I hope names are self-explanatory.

In few words: Each new employee added makes it about 300 miliseconds slower. So I have to refactor.

Following advice this link: https://stackoverflow.com/questions/18144782/performance-of-foreach-array-map-with-lambda-and-array-map-with-static-function I map two Collections with nested loops like this:

foreach( Collection1 as key1 => $element) {
    foreach(Collection2 as key => $item {
        If (any condition) {
            Do something;

However, since the post has a couple of years I wonder if still two nested loops are faster than using PHP Maps functions.

Is there any “magic method” in Laravel that permit map two collections in a faster way or we have to rely on pure PHP methods and speed?

Please sign in or create an account to participate in this conversation.