#5742 - Consider in_array when there are 3 or more string comparisons
| Identifier | #5742 |
|---|---|
| Issue type | Feature request or suggestion |
| Title | Consider in_array when there are 3 or more string comparisons |
| Status | Open |
| Tags |
Roadmap: Over the horizon (custom) Type: Performance (custom) |
| Handling member | Deleted |
| Addon | core |
| Description | in_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. |
| Steps to reproduce | |
| Funded? | No |
The system will post a comment when this issue is modified (e.g., status changes). To be notified of this, click "Enable comment notifications".
Comments
<?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)