Unknown Classes Beginner Since Laravel 10.x

Benchmark::dd()

Time one or more closures and dump the results — the quickest way to compare performance of different approaches.

Overview

Illuminate\Support\Benchmark provides a clean API for timing code execution. The dd() method times closures and dumps the results immediately — perfect for quick performance comparisons during development.

Usage

Time a single operation:

use Illuminate\Support\Benchmark;

Benchmark::dd(fn () => User::count());
// "1.204ms"

Compare multiple approaches side by side:

Benchmark::dd([
    'count()' => fn () => User::count(),
    'query'   => fn () => DB::table('users')->count(),
    'raw'     => fn () => DB::select('SELECT COUNT(*) FROM users'),
]);
// [
//     'count()' => '1.204ms',
//     'query'   => '0.562ms',
//     'raw'     => '0.401ms',
// ]

If you need the timing data without dumping, use measure() instead:

$ms = Benchmark::measure(fn () => sleep(1));
// 1000.0 (float, in milliseconds)

When to Use

  • Quick A/B performance tests during development
  • Comparing Eloquent vs raw query performance
  • Profiling specific blocks of code without a full profiler