Issue Task Closed

#55626Replace GeneralUtility::inList with isset() for lists of values being static or within loops

Jo Hasenau 02/03/14, 15:09 updated 10/02/18, 12:08

Description

There are about 250 places in the core making use of GeneralUtility::inList

GeneralUtility::inList('formtype_mail,subject,html_enabled', $confData['fieldname']));
GeneralUtility::inList($listToCheck, $confData['fieldname']));

On the one hand this is quite fast and handy, when you are checking variable lists of values just once.
But while checking variable lists that are used within a loop or static lists of know values, it would be much faster and less memory consuming to make use of an array with appropriate keys and isset();

Just two simple use cases with loops:

$listArrayToCheck = array_flip(explode(',', $listToCheck));
foreach($whatever as $value) {
  if(isset($listArrayToCheck[$value])) {}
}

or

$listArrayToCheck = array_flip(array(
    'formtype_mail', 'subject', 'html_enabled'
));
foreach($whatever as $value) {
  if(isset($listArrayToCheck[$value])) {}
}

would be better than

foreach($whatever as $value) {
  GeneralUtility::inList($listToCheck, $value);
}

And a use case for a single check of a known static list:

$listArrayToCheck = array_flip(array(
    'formtype_mail', 'subject', 'html_enabled'
));
if(isset($listArrayToCheck[$value]));

So the rule of thumb should be to use GeneralUtility::inList for single checks of lists with unknown values only.
Of course the array_flip could be avoided as well for the use case with known list values, but this would sacrifice readability for performance.

Reviews 7