'Which is the best/fastest practice? Create the same loop in each case of a switch statement, or to switch the same case of a for loop?

So I was about to write something like following code

switch ($mode) {
    case 'all':
        foreach ($sortInfo as $info) $filter[] = array_merge([$info->maincat], $info->subcats);
        break;
    case 'sub':
        foreach ($sortInfo as $info) $filter[] = $info->subcats;
        break;
    default:
        foreach ($sortInfo as $info) $filter[] = [$info->maincat];
        break;
}

Then I told myself "hey, wouldn't it be more optimized if I wrapped the whole switch inside the for loop?"

foreach ($sortInfo as $info) {
    switch ($mode) {
        case 'all':
            $filter[] = array_merge([$info->maincat], $info->subcats);
            break;
        case 'sub':
            $filter[] = $info->subcats;
            break;
        default:
            $filter[] = [$info->maincat];
            break;
    }
}

But while it does technically save a bit (get it?) of filesize, the loop would confirm the same information in the switch statement for each iteration of the loop.

I figured there is no objective way to determine which is fastest because it depends on the length of $sortinfo, but since it's the first time I come across this dilemma, I'd like to know if there's a preferred method, or what's your take on it.



Solution 1:[1]

Okay, it looks like the asker is not going to respond to my request for sample data and expected result.

To minimize the number of iterated conditions, you can determine if the mode is "sub", and simply collect that column of data (without any convoluted handling).

Otherwise, it is known that "maincat" data will be required in the result. The only thing to determine is if "subcats" should be pushed into the result (PER ROW -- otherwise the "maincat" and "subcats" relationships will be destroyed).

This is direct and concise, but it doesn't enjoy optimal maintainability like a switch() or match() block would. If you don't expect to extend the functionality of the snippet, I'd use a condition before the loop and a condition within the loop.

Code: (no demo because no sample data was provided)

if ($mode === 'sub') {
    $filter = array_column($sortInfo, 'subcats');
} else {
    $filter = [];
    foreach ($sortInfo as $info) {
        $filter[] = [$info->maincat, ...($mode === 'all' ? $info->subcats : [])];
    }
}

Solution 2:[2]

Basicly, the code and outcome are the same, but i think the first one would be faster because it is more simple and straight-forward. The second one looks cleaner but it needs to switch-case and make decision everytime it loop and this would slow down the whole process.

Solution 3:[3]

<p *ngIf = "dynamicData.value"> 'Dynamic data value is equal' {{dynamicData.value}} </p>
<p *ngIf = "!dynamicData.value"> 'no dynamic data' </p>

Solution 4:[4]

You can use variable in your .ts file or you can use + interpolation 'Dynamic data value is equal: ' + dynamicData.default.

But I like more answer posted by FedMice ;)

Solution 5:[5]

I hope this works.

 <p>{{ dynamicData.value ? (`Dynamic data value is equal: ${dynamicData?.value}`) : `no dynamic data`}}</p>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mickmackusa
Solution 2 Hey
Solution 3 FedMice
Solution 4 Yodde
Solution 5 fasihshaikh