It appears that ($somearr[testindex]=== NULL) is comperable to isset() for speed (both of which are EXTREMELY faster than array_key_exists()). Here is some code that I used for the comparisons.
<?php
for(some_loop_here with an increasingly larger array being searched)
{
benchMarkThis("arraykeyexists",1);
$foo = array_key_exists($newfriend,$list);
benchMarkThis("arraykeyexists");
benchMarkThis("eqeqeqnull",1);
$foo = ($list[$newfriend] === NULL);
benchMarkThis("eqeqeqnull");
benchMarkThis("isset",1);
$foo = isset($list[$newfriend]);
benchMarkThis("isset");
}
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function benchMarkThis($bmname, $reset=0)
{
static $lastTime = array();
static $totalTime = array();
if ($reset == 1) {
$lastTime[$bmname] = getmicrotime();
return;
}
$now = getmicrotime();
$diff = $now - $lastTime[$bmname];
$diff = round($diff, 6);
$totalTime[$bmname] += $diff;
echo "$bmname time=$diff, elapsed=$totalTime[$bmname]<br>";
$lastTime[$bname] = $now;
}
?>
Ouput (last iteration):
eqeqeqnull time=0.000126, elapsed=0.993481
isset time=0.000119, elapsed=0.768909
arraykeyexists time=0.011609, elapsed=52.58483
Moral of the story: if you can get away with it, do NOT use array_key_exists().