Req #71239 [NEW]: Language construct for array_key_exists() (performance)
| From: | andreas at dqxtech dot net | Date: | Tue, 29 Dec 2015 17:24:22 +0000 |
| Subject: | Req #71239 [NEW]: Language construct for array_key_exists() (performance) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-198283@lists.php.net to get a copy of this message | ||
From: andreas at dqxtech dot net
Operating system:
PHP version: 7.0.2RC1
Package: Arrays related
Bug Type: Feature/Change Request
Bug description:Language construct for array_key_exists() (performance)
Description:
------------
There are places on the web telling me that array_key_exists($k, $a) is
slower than isset($a[$k]).
https://startpage.com/do/search?query=php+performance+isset+vs+array_key_exists&cat=web&pl=chrome&language=english
Tests with PHP 5.5.9 confirm this, it seems that array_key_exists() uses
almost 4x the time of isset().
On the other hand: Semantically, array_key_exists() is preferable over
isset().
If you already know that $a is an array and not null, and that $k is a
valid array key, then you don't want to have this tested again. You want
TRUE or FALSE if the variables are set, and a warning (and FALSE, I
guess) otherwise.
This allows the IDE to warn you if you test a variable that is possibly
undefined, or possibly not an array. With isset(), the IDE will keep
silent, which can lead to hidden bugs.
In theory, array_key_exists() should be faster than isset(), because
there are fewer cases to consider.. but it is not, because one is a
function and the other a "language construct".
Aside of that, there is of course the difference of how NULL values are
handled.. In fact, in an ideal world, there would be two versions of
array_key_exists(). Both with the performance of a language construct.
But with a different behavior for NULL values.
--
Edit bug report at https://bugs.php.net/bug.php?id=71239&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71239&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71239&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71239&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71239&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71239&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71239&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71239&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71239&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71239&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71239&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71239&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71239&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71239&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71239&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71239&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71239&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71239&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71239&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71239&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71239&r=mysqlcfg