1.2.998. Processing Collector¶
When accumulating data in a variable, within a loop, it is slow to apply repeatedly a function to the variable.
The example below illustrate the problem : $collector
is build with element from $array
. $collector
actually gets larger and larger, slowing the in_array() call each time.
It is better to apply the preg_replace() to $a
, a short variable, and then, add $a
to the collector.
<?php
// Fast way
$collector = '';
foreach($array as $a){
$a = preg_replace('/__(.*?)__/', '<b>$1</b>', $a);
$collector .= $a;
}
// Slow way
$collector = '';
foreach($array as $a){
$collector .= $a;
$collector = preg_replace('/__(.*?)__/', '<b>$1</b>', $collector);
}
?>
1.2.998.1. Suggestions¶
Avoid applying the checks on the whole data, rather on the diff only.
1.2.998.2. Specs¶
Short name |
Performances/RegexOnCollector |
Rulesets |
|
Exakat since |
1.2.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
Very high |
Available in |