Req #71239 [Opn->Csd]: Language construct for array_key_exists() (performance)

From: Date: Wed, 29 Jan 2020 15:57:11 +0000
Subject: Req #71239 [Opn->Csd]: Language construct for array_key_exists() (performance)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225223@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71239&edit=1 ID: 71239 Updated by: nikic@php.net Reported by: andreas at dqxtech dot net Summary: Language construct for array_key_exists() (performance) -Status: Open +Status: Closed Type: Feature/Change Request Package: Arrays related PHP Version: 7.0.2RC1 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: array_key_exists() has an optimized opcode implementation since PHP 7.4. Previous Comments: ------------------------------------------------------------------------ [2018-08-23 00:50:31] carusogabriel@php.net Related to Bug #76148 ------------------------------------------------------------------------ [2018-08-23 00:49:58] carusogabriel@php.net Related to Bug #71239 ------------------------------------------------------------------------ [2018-08-23 00:49:58] carusogabriel@php.net Related To: Bug #71239 ------------------------------------------------------------------------ [2015-12-29 17:24:17] andreas at dqxtech dot net 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 this bug report at https://bugs.php.net/bug.php?id=71239&edit=1

« previous php.bugs (#225223) next »