View Issue Details

IDProjectCategoryView StatusLast Update
0005742Composrcorepublic2024-07-22 21:22
ReporterPatrick SchmalstigAssigned To 
SeverityFeature-request 
Status non-assignedResolutionopen 
Product Version 
Fixed in Version 
Summary0005742: Consider in_array when there are 3 or more string comparisons
Descriptionin_array is more readable and possibly faster if comparing 3 or more strings with the same condition in an if guard.

It can also be used for strict comparison of multiple strings (with in_array parameter 3 being set to true).

Consider adding this to coding standards / code linter.
TagsRoadmap: Over the horizon, Type: Performance
Time estimation (hours)
Sponsorship open

Activities

Chris Graham

2024-07-22 21:22

administrator   ~0008877

Hmm, I'm surprised but it is faster...

<?php

define('ITERATIONS', 10000000);

$x = 'hello';

$a = microtime(true);
for ($i = 0; $i < ITERATIONS; $i++) {
    if ($x == 'a' || $x == 'b' || $x == 'c') {
    }
}
$b = microtime(true);
var_dump($b - $a);

$a = microtime(true);
for ($i = 0; $i < ITERATIONS; $i++) {
        if (in_array($x, ['a', 'b', 'c'])) {
        }
}
$b = microtime(true);
var_dump($b - $a);


float(0.28515386581421)
float(0.10288000106812)

Issue History

Date Modified Username Field Change
2024-04-28 19:16 Patrick Schmalstig New Issue
2024-04-28 19:16 Patrick Schmalstig Tag Attached: Roadmap: Over the horizon
2024-07-22 21:22 Chris Graham Tag Attached: Type: Performance
2024-07-22 21:22 Chris Graham Note Added: 0008877