This question is merely for me as I always like to write optimized code that can run also on cheap slow servers (or servers with A LOT of traffic)
I looked around and I was not able to find an answer. I was wondering what is faster between those two examples keeping in mind that the array's keys in my case are not important (pseudo-code naturally):
<?php
$a = array();
while($new_val = 'get over 100k email addresses already lowercased'){
if(!in_array($new_val, $a){
$a[] = $new_val;
//do other stuff
}
}
?>
<?php
$a = array();
while($new_val = 'get over 100k email addresses already lowercased'){
if(!isset($a[$new_val]){
$a[$new_val] = true;
//do other stuff
}
}
?>
As the point of the question is not the array collision, I would like to add that if you are afraid of colliding inserts for $a[$new_value]
, you can use $a[md5($new_value)]
. it can still cause collisions, but would take away from a possible DoS attack when reading from a user provided file (http://nikic.github.com/2011/12/28/Supercolliding-a-PHP-array.html)