1.2.1460. array_key_exists() Speedup

array_key_exists() has its own opcode, leading to better features and speed.

isset() is faster for all non-empty values, but is limited when the value is NULL or empty : then, array_key_exists() has the good features.

This change makes `array_key_exists() <https://www.php.net/array_key_exists>`_ actually faster than `isset() <https://www.www.php.net/isset>`_ by ~25% (tested with GCC 8, -O3, march=native, mtune=native)..

<?php

$foo = [123 => 456];

// This is sufficient and efficient since PHP 7.4
if (array_search_key($foo[123])) {
    // do something
}

// taking advantages of performances for PHP 7.4 and older
if (isset($foo[123]) || array_search_key($foo[123])) {
    // do something
}

?>

See also Implement ZEND_ARRAY_KEY_EXISTS opcode to speed up array_key_exists().

1.2.1460.1. Suggestions

  • Remove the isset() call and the logical operator

1.2.1460.2. Specs

Short name

Performances/ArrayKeyExistsSpeedup

Rulesets

All, Changed Behavior, Performances, Suggestions

Exakat since

1.6.1

PHP Version

With PHP 7.4 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

opcode

Available in

Entreprise Edition, Exakat Cloud