Skip to content

Instantly share code, notes, and snippets.

@stollr
Last active December 3, 2025 10:07
Show Gist options
  • Select an option

  • Save stollr/84ebb860a4dd5f333d90b5fbefad1b2e to your computer and use it in GitHub Desktop.

Select an option

Save stollr/84ebb860a4dd5f333d90b5fbefad1b2e to your computer and use it in GitHub Desktop.
Array emptyness check benchmark

$ php8.4 test.php

Check emptyness of 1 million empty arrays 
    with empty($array): 0.0091989040374756
    with count($array) === 0: 0.01491904258728
    with [] === $array: 0.012963056564331

Check emptyness of 1 million non-empty arrays 
    with empty($array): 0.011882781982422
    with count($array) === 0: 0.016752004623413
    with [] === $array: 0.019222021102905

$ php8.5 test.php

Check emptyness of 1 million empty arrays 
    with empty($array): 0.0097339153289795
    with count($array) === 0: 0.01529598236084
    with [] === $array: 0.012676000595093

Check emptyness of 1 million non-empty arrays 
    with empty($array): 0.012006998062134
    with count($array) === 0: 0.017817974090576
    with [] === $array: 0.019315958023071
<?php
$emptyArrays = array_fill(0, 1_000_000, []);
$filledArrays = array_fill(0, 1_000_000, [1,2,3]);
echo 'Check emptyness of 1 million empty arrays '.PHP_EOL;
$time = microtime(true);
foreach ($emptyArrays as $emptyArray) {
empty($emptyArray);
}
echo ' with empty($array): '.(microtime(true)-$time).PHP_EOL;
$time = microtime(true);
foreach ($emptyArrays as $emptyArray) {
count($emptyArray) === 0;
}
echo ' with count($array) === 0: '.(microtime(true)-$time).PHP_EOL;
$time = microtime(true);
foreach ($emptyArrays as $emptyArray) {
[] === $emptyArray;
}
echo ' with [] === $array: '.(microtime(true)-$time).PHP_EOL;
echo PHP_EOL.'Check emptyness of 1 million non-empty arrays '.PHP_EOL;
$time = microtime(true);
foreach ($filledArrays as $array) {
empty($array);
}
echo ' with empty($array): '.(microtime(true)-$time).PHP_EOL;
$time = microtime(true);
foreach ($filledArrays as $array) {
count($array) === 0;
}
echo ' with count($array) === 0: '.(microtime(true)-$time).PHP_EOL;
$time = microtime(true);
foreach ($filledArrays as $array) {
[] === $array;
}
echo ' with [] === $array: '.(microtime(true)-$time).PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment