1.2.1131. Slow Functions¶
Avoid using those slow native PHP functions, and replace them with alternatives.
Slow Function |
Faster |
array_diff() array_intersect() array_key_exists() array_map() array_search() array_udiff() array_uintersect() array_unshift() array_walk() in_array() preg_replace() strstr() uasort() uksort() usort() array_unique() |
foreach() foreach() isset() and array_key_exists() foreach() array_flip() and isset() Use another way Use another way Use another way foreach() isset() strpos() strpos() Use another way Use another way Use another way array_keys() and array_count_values() |
array_unique() has been accelerated in PHP 7.2 and may be used directly from this version on : Optimize `array_unique() <https://github.com/php/php-src/commit/6c2c7a023da4223e41fea0225c51a417fc8eb10d>`_.
array_key_exists() has been accelerated in PHP 7.4 and may be used directly from this version on : Implement ZEND_ARRAY_KEY_EXISTS opcode to speed up `array_key_exists() <https://github.com/php/php-src/pull/3360>`_.
<?php
$array = source();
// Slow extraction of distinct values
$array = array_unique($array);
// Much faster extraction of distinct values
$array = array_keys(array_count_values($array));
?>
1.2.1131.1. Suggestions¶
Replace the slow function with a faster version
Remove the usage of the slow function
1.2.1131.2. Specs¶
Short name |
Performances/SlowFunctions |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Major |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
ClearPHP |
|
Examples |
|
Available in |