Laravel Benchmark helper makes improving PHP performance simple. Learn how to measure code execution time and optimize your app with data-driven insights.
Last month, I spent about two days tracking down a slowdown in a reporting module that was taking roughly 450ms to generate. I had a hunch that a specific Eloquent query was the culprit, but I was just guessing, and guessing is a fast track to technical debt. That’s when I turned to the Benchmark helper, and it completely changed how I approach performance testing.
When you're building features, it’s easy to fall into the trap of "premature optimization." You might think a complex collect() chain is faster than a standard foreach loop, but without measuring, you're just making assumptions. PHP performance isn't always intuitive, especially when you factor in how the engine handles memory and object instantiation.
The Illuminate\Support\Benchmark class, introduced in Laravel 9.x, provides a clean, fluent way to measure exactly how long your code blocks take to run. It removes the guesswork from your development process.
The simplest way to use it is by passing a closure to the measure method. It returns the time taken in milliseconds.
PHPuse Illuminate\Support\Benchmark; $time = Benchmark::measure(fn() => User::all()); dump($time);
That’s it. You get a float value representing the time. But simply knowing one block takes 50ms isn't always helpful on its own. You usually need to compare it against an alternative to see if your "optimization" actually helped.
This is where the tool really shines. If you're deciding between two different ways to process a collection, you can pass an array of closures to the dd method. Laravel will run both and output the results directly to your screen.
PHPBenchmark::dd([ 'Eloquent' => fn() => User::where('active', 1)->get(), 'Query Builder' => fn() => DB::table('users')->where('active', 1)->get(), ]);
When you run this, you'll see a clean output showing exactly how long each approach took. I've used this to decide between using Mastering Laravel Cache: A Beginner's Guide to Performance and fetching fresh data. Sometimes, the overhead of checking the cache store is actually slower than a well-indexed database query.
Before you get carried away, remember that Benchmark measures execution time, not memory usage or database load. I once spent an afternoon optimizing a loop that saved 2ms, only to realize I had increased the memory footprint by 15MB because I was eager-loading data I didn't actually need.
Always keep these three rules in mind when optimizing:
I rarely use the dd() helper in production, as it kills the request. If you need to monitor performance in the wild, you’re better off using logs or a dedicated APM tool. However, for local development or staging, Benchmark is your best friend.
If you find yourself needing to clean up your controllers while you optimize, I highly recommend checking out Laravel Form Requests: Clean Controller Validation Guide. Keeping your code readable is just as important as keeping it fast.
Does the Benchmark helper account for framework overhead? Yes, it measures the code inside the closure. If you wrap a whole request, you're measuring the entire lifecycle, but it's usually more effective to measure specific, isolated chunks of logic.
Is it accurate for very small code snippets? For extremely small snippets, the variation in PHP's internal state might cause noise. If you're measuring something that takes 0.001ms, you'll see inconsistent results. Focus on measuring business logic that actually consumes significant time.
Can I use it to test database queries? Absolutely. Just wrap your query in the closure. Just be aware that the first run might be slower due to database connection initialization, so you might want to run a "warm-up" iteration if you're looking for precise numbers.
Using the Benchmark helper is a low-effort, high-reward habit. Next time you're debating which approach to take, just write the test. You'll often be surprised by the results. I'm still learning how different PHP versions affect these benchmarks, and I'm honestly always a bit skeptical of my own code until I see the numbers. Don't be afraid to experiment—that's how you get better at this.
Laravel form validation custom error messages are essential for great UX. Learn how to use FormRequest to provide clear, helpful feedback to your users today.