Skip to content

Instantly share code, notes, and snippets.

@e0ipso
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e0ipso/11578357e02022df0c0e to your computer and use it in GitHub Desktop.
Save e0ipso/11578357e02022df0c0e to your computer and use it in GitHub Desktop.
empty vs array_key_exists
<?php
define('ITERATIONS', 10000000);
$array = array(
'foo' => 'bar',
'baz' => 'zzz',
);
for ($i = 0; $i < ITERATIONS; $i++) {
array_key_exists('foo', $array);
array_key_exists('invalid', $array);
}
print 'Finished' . PHP_EOL;
<?php
define('ITERATIONS', 10000000);
$array = array(
'foo' => 'bar',
'baz' => 'zzz',
);
for ($i = 0; $i < ITERATIONS; $i++) {
empty($array['foo']);
empty($array['invalid']);
}
print 'Finished' . PHP_EOL;
-----------------------------------------------------------------------
⮀ time php array_key_exists.php
Finished
php array_key_exists.php 20.92s user 0.20s system 99% cpu 21.185 total
-----------------------------------------------------------------------
-----------------------------------------------------------------------
⮀ time php empty.php
Finished
php empty.php 2.90s user 0.03s system 98% cpu 2.982 total
-----------------------------------------------------------------------
-----------------------------------------------------------------------
⮀ time php empty.php
Finished
php empty.php 2.91s user 0.04s system 98% cpu 2.995 total
-----------------------------------------------------------------------
-----------------------------------------------------------------------
⮀ time php array_key_exists.php
Finished
php array_key_exists.php 19.22s user 0.17s system 99% cpu 19.460 total
-----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment