diff --git a/CHANGELOG.md b/CHANGELOG.md index c8d6f4f..015b7e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Fixed the compiled runtime to evaluate sub-expressions when the left side is null. * Fixed the ordering operators to return null instead of false for non-numbers. * Fixed aggregate and sort functions to error on an invalid first or only element. +* Improved the performance of type checks on large arrays. ## 2.9.3 - 2026-11-16 diff --git a/src/Utils.php b/src/Utils.php index 9ed3860..9ad207c 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -46,8 +46,7 @@ public static function type($arg) if (empty($arg)) { return 'array'; } - reset($arg); - return key($arg) === 0 ? 'array' : 'object'; + return self::firstKey($arg) === 0 ? 'array' : 'object'; } elseif ($arg instanceof \stdClass) { return 'object'; } elseif ($arg instanceof \Closure) { @@ -83,7 +82,7 @@ public static function type($arg) public static function isObject($value) { if (is_array($value)) { - return !$value || array_keys($value)[0] !== 0; + return !$value || self::firstKey($value) !== 0; } // Handle array-like values. Must be empty or offset 0 does not exist @@ -102,7 +101,7 @@ public static function isObject($value) public static function isArray($value) { if (is_array($value)) { - return !$value || array_keys($value)[0] === 0; + return !$value || self::firstKey($value) === 0; } // Handle array-like values. Must be empty or offset 0 exists. @@ -111,6 +110,15 @@ public static function isArray($value) : false; } + private static function firstKey(array $array) + { + foreach ($array as $key => $value) { + return $key; + } + + return null; + } + /** * JSON-semantic equality: one number type, structural comparison for arrays * and objects, and no object key-order sensitivity. diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 427b896..0f06c43 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -19,6 +19,7 @@ public static function typeProvider(): array [[], 'array'], [[1, 2], 'array'], [['a' => 1], 'object'], + [['a' => 1, 0 => 2], 'object'], [new \stdClass(), 'object'], [function () {}, 'expression'], [new \ArrayObject(), 'array'], @@ -80,6 +81,7 @@ public static function isArrayProvider(): array [[], true], [[1, 2], true], [['a' => 1], false], + [['a' => 1, 0 => 2], false], [new _TestClass(), false], [new \ArrayObject(['a' => 'b']), false], [new \ArrayObject([1]), true], @@ -101,6 +103,7 @@ public static function isObjectProvider(): array [[], true], [[1, 2], false], [['a' => 1], true], + [['a' => 1, 0 => 2], true], [new _TestClass(), false], [new \ArrayObject(['a' => 'b']), true], [new \ArrayObject([1]), false], @@ -116,6 +119,16 @@ public function testChecksIfObject($given, $result): void $this->assertSame($result, Utils::isObject($given)); } + public function testTypeChecksIgnoreInternalPointer(): void + { + $list = [1, 2]; + end($list); + + $this->assertSame('array', Utils::type($list)); + $this->assertTrue(Utils::isArray($list)); + $this->assertFalse(Utils::isObject($list)); + } + public static function isTruthyProvider(): array { return [