There are lots of methods making use of the costly trimExplode method using a foreach loop on the exploded array immediately after the method call.
The result is an array_map plus at least one (sometimes a second) foreach loop over the array of exploded items, one (or two) inside the trimExplode and one outside over the resulting array. Performancewise foreach is not just faster in most of the cases but has a much smaller memory footprint, so it would be a good idea to just stay with trimExplode when this is useful and necessary.
Since most of the cases don't make use of the limit parameter of trimExplode, we should replace any call to that method based on the following rules:
- It must not be replaced when there is a limit in the original method call
- It must not be replaced when there is no foreach loop making use of the array immediately after the method call
- It must be replaced by at least a trim within the following loop
- It must be replaced by a trim AND a check for empty values within the following loop, when the third parameter was set to TRUE before
#1 Updated by Michiel Roos over 6 years ago
Here are some benchmarks http://pastebin.com/SZjWDDk9
preg_split is quite fast for 'most' cases.
We should indeed inspect all calls and see we actually need to trim the values.The following methods do not handle whitespace:
Code using these methods should not be calling trimExplode, but just plain explode.
I have not done any inspection yet.