Quickly Measure Performance in Laravel Using Benchmark
Laravel’s Benchmark utility helps developers measure execution time of code blocks in milliseconds. This article explains how to use it with simple, real-world project examples to identify performance bottlenecks quickly.
Real Project Example: Finding a Slow Query
Imagine you’re working on an admin dashboard and want to know which query is faster.
use Illuminate\Support\Benchmark;
use App\Models\User;
Benchmark::dd([
'Using count()' => fn () => User::count(),
'Using all()->count()' => fn () => User::all()->count(),
]);
Output (example):
Using count(): 0.5 ms
Using all()->count(): 18.7 ms
Instant clarity — no guessing.