Skip to content

Instantly share code, notes, and snippets.

@JohnRoux
Created June 11, 2024 17:16
Show Gist options
  • Save JohnRoux/024ddb44644ae904b56a1a04faa1deb1 to your computer and use it in GitHub Desktop.
Save JohnRoux/024ddb44644ae904b56a1a04faa1deb1 to your computer and use it in GitHub Desktop.
app:test-array-speed
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Benchmark;
class TestArraySpeed extends Command
{
protected $signature = 'app:test-array-speed';
protected $description = 'Command description';
public const ARRAY = [
"app" => [
"url" => "https://app.com",
"maintenance" => [
"driver" => false,
]
]
];
public function handle()
{
$iterations = collect([10, 100, 1000, 10000, 100000]);
$data = [];
$iterations->each(function ($iterations) use (&$data)
{
$data[] = [
$iterations,
Benchmark::measure($this->findInArray(), $iterations) * 1000,
Benchmark::measure($this->findInArrGet(), $iterations) * 1000,
Benchmark::measure($this->findInEnum(), $iterations) * 1000
];
});
$this->table(['Iterations', 'Array Time', 'ArrGet Time', 'Enum Time'], $data);
}
public function findInArray()
{
return fn() => self::ARRAY['app'];
}
public function findInArrGet()
{
return fn() => \Illuminate\Support\Arr::get(self::ARRAY, 'app');
}
public function findInEnum()
{
return fn() => App::Url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment