---
title: 'Benchmark Eloquent and query builder code'
description: 'Run equivalent Eloquent and query builder snippets several times in LaraBench, then compare timings, SQL, and returned rows.'
canonical_url: 'https://larabench.com/laravel-tinker-workflows/benchmark-eloquent-queries'
md_url: 'https://larabench.com/laravel-tinker-workflows/benchmark-eloquent-queries.md'
last_updated: '2026-07-28'
---
# Benchmark Eloquent and query builder code

> Run equivalent Eloquent and query builder snippets several times in LaraBench, then compare timings, SQL, and returned rows.

## Direct Answer

Make the Eloquent and query builder snippets return the same shape, run each several times against the same connection, and compare the timing distribution rather than a single fastest run.

## When To Use This

Use this after you already know what query you want to test. A benchmark gives a local timing signal for a specific app, dataset, cache state, and database server.

## Terminal Approach

Laravel's `Benchmark` helper measures a callback in milliseconds. Run each complete snippet with the same iteration count, result size, and selected columns.

```php
use Illuminate\Support\Benchmark;

Benchmark::measure(
    fn () => App\Models\Order::query()
        ->where('status', 'paid')
        ->latest()
        ->limit(50)
        ->get(['id', 'total', 'created_at']),
    iterations: 10,
);
```

## LaraBench Approach

Open Benchmarks, create one benchmark for each snippet, use the same connection and repeat count, then compare median and outlier runs.

## Snippets

```php
use App\Models\Order;

return Order::query()
    ->where('status', 'paid')
    ->latest()
    ->limit(50)
    ->get(['id', 'total', 'created_at'])
    ->map->only(['id', 'total', 'created_at']);
```

```php
use Illuminate\Support\Facades\DB;

return DB::table('orders')
    ->where('status', 'paid')
    ->latest()
    ->limit(50)
    ->get(['id', 'total', 'created_at']);
```

## Expected Result

Both snippets should return the same rows and fields. There is no universal winner between Eloquent and the query builder; inspect SQL and output shape before changing production code.

## Troubleshooting

- If the result differs, compare global scopes, casts, accessors, and selected columns.
- If the first run is slower, rerun both snippets after the app and database have warmed up.
- If the benchmark is noisy, reduce the work, run more repeats, or move the test closer to the database.

## Safety Notes

Benchmark read-only code first. Repeated runs can multiply writes, dispatches, emails, cache mutations, or external API calls.

## Related Sources

- [Laravel query builder documentation](https://laravel.com/docs/13.x/queries)
- [Laravel Eloquent documentation](https://laravel.com/docs/13.x/eloquent)
- [LaraBench Benchmarks docs](https://larabench.com/docs/advanced-usage/benchmarks)

## Related Workflows

- [Find N+1 queries in a Laravel snippet](https://larabench.com/laravel-tinker-workflows/find-laravel-n-plus-one-queries)
- [Inspect Eloquent models and collections](https://larabench.com/laravel-tinker-workflows/inspect-eloquent-models-and-collections)

## Canonical Page

- [HTML page](https://larabench.com/laravel-tinker-workflows/benchmark-eloquent-queries)
- [Markdown mirror](https://larabench.com/laravel-tinker-workflows/benchmark-eloquent-queries.md)

## Glossary

- [Laravel workflow glossary](https://larabench.com/docs/reference/glossary)

## Sitemap

See the full [sitemap](https://larabench.com/sitemap.md) for all public pages.
