/Dev/PHP
Last modified: July 22 2025 13:37:55
Laravel’s expressive, feature-rich layers come at the cost of some extra overhead compared to “plain” or “core” PHP. Below are several common scenarios where you’ll typically see Laravel slower than equivalent core-PHP code, along with illustrative examples:
1. Framework Bootstrapping & Routing
Why it’s slower:
Every request in Laravel must bootstrap the entire framework—load service providers, configuration, middleware, etc.—before even dispatching your route.
// core PHP (public/index.php)
$start = microtime(true);
if ($_SERVER['REQUEST_URI'] === '/hello') {
echo 'Hello, world!';
}
$time = microtime(true) - $start;
error_log("core PHP boot + dispatch: {$time}");
# Laravel (public/index.php → handled by /routes/web.php)
Route::get('/hello', function () {
return 'Hello, world!';
});
// Measured in public/index.php:
$start = microtime(true);
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$response = $app->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$app->terminate($request, $response);
$time = microtime(true) - $start;
error_log("Laravel boot + dispatch: {$time}");
Typical result:
– Core PHP: \~0.5 ms
– Laravel: \~10–20 ms
The bulk of that difference is framework initialization and routing logic.
2. Simple Database Queries: PDO vs Eloquent
Why it’s slower:
Eloquent adds layers (models, relationships, collections) that make each query more convenient—but heavier.
// core PHP using PDO
$pdo = new PDO($dsn, $user, $pass);
$start = microtime(true);
$stmt = $pdo->query('SELECT * FROM posts WHERE id = 1');
$post = $stmt->fetch(PDO::FETCH_ASSOC);
$time = microtime(true) - $start;
error_log("PDO query: {$time}");
// Laravel using Eloquent
use App\Models\Post;
$start = microtime(true);
$post = Post::find(1);
$time = microtime(true) - $start;
Log::info("Eloquent find: {$time}");
Typical result:
– PDO raw fetch: \~1 ms
– Eloquent → Model hydration: \~3–5 ms
3. Bulk Inserts
Why it’s slower:
Inserting many rows one-by-one with Eloquent invokes events, mass-assignment checks, and individual queries.
// core PHP bulk insert
$data = [];
for ($i = 0; $i < 1000; $i++) {
$data[] = "(?, ?)";
$bindings[] = "Title {$i}";
$bindings[] = "Body {$i}";
}
$sql = "INSERT INTO posts (title, body) VALUES " . implode(',', $data);
$start = microtime(true);
$pdo->prepare($sql)->execute($bindings);
$time = microtime(true) - $start;
error_log("PDO bulk insert: {$time}");
// Laravel Eloquent looped insert
$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
Post::create([
'title' => "Title {$i}",
'body' => "Body {$i}",
]);
}
$time = microtime(true) - $start;
Log::info("Eloquent 1000× create: {$time}");
Typical result:
– PDO single bulk statement: \~15–30 ms
– Eloquent in a loop: \~800–1200 ms
Tip: For large datasets in Laravel, use the query builder’s insert() with a batch array:
Post::insert($arrayOf1000Rows);
4. View Rendering
Why it’s slower:
Blade templates are compiled to PHP then rendered, and include features like sections, inheritance, and directives.
// core PHP simple include
$start = microtime(true);
extract(['name' => 'Alice']);
include 'views/hello.php'; // just: <?php echo "Hello, $name"; ?>
$time = microtime(true) - $start;
error_log("PHP include view: {$time}");
// Laravel Blade
// resources/views/hello.blade.php
// Hello, {{ $name }}
$start = microtime(true);
return view('hello', ['name' => 'Alice']);
$time = microtime(true) - $start;
Log::info("Blade render: {$time}");
Typical result:
– PHP include: \~0.2 ms
– Blade render: \~2–4 ms
5. Middleware Pipeline
Why it’s slower:
Each middleware class adds a function call and possibly logic (authentication, CSRF, logging, etc.) before reaching your controller.
/Dev/PHP (Last modified: July 22 2025 13:37:55 )Laravel’s expressive, feature-rich layers come at the cost of some extra overhead compared to “plain” or “core” PHP. Below are several common scenarios where you’ll typically see Laravel slower than equivalent core-PHP code, along with illustrative examples:
1. Framework Bootstrapping & Routing
Why it’s slower:
Every request in Laravel must bootstrap the entire framework—load service providers, configuration, middleware, etc.—before even dispatching your route.
// core PHP (public/index.php)
$start = microtime(true);
if ($_SERVER['REQUEST_URI'] === '/hello') {
echo 'Hello, world!';
}
$time = microtime(true) - $start;
error_log("core PHP boot + dispatch: {$time}");
# Laravel (public/index.php → handled by /routes/web.php)
Route::get('/hello', function () {
return 'Hello, world!';
});
// Measured in public/index.php:
$start = microtime(true);
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$response = $app->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$app->terminate($request, $response);
$time = microtime(true) - $start;
error_log("Laravel boot + dispatch: {$time}");
Typical result:
– Core PHP: \~0.5 ms
– Laravel: \~10–20 ms
The bulk of that difference is framework initialization and routing logic.
2. Simple Database Queries: PDO vs Eloquent
Why it’s slower:
Eloquent adds layers (models, relationships, collections) that make each query more convenient—but heavier.
// core PHP using PDO
$pdo = new PDO($dsn, $user, $pass);
$start = microtime(true);
$stmt = $pdo->query('SELECT * FROM posts WHERE id = 1');
$post = $stmt->fetch(PDO::FETCH_ASSOC);
$time = microtime(true) - $start;
error_log("PDO query: {$time}");
// Laravel using Eloquent
use App\Models\Post;
$start = microtime(true);
$post = Post::find(1);
$time = microtime(true) - $start;
Log::info("Eloquent find: {$time}");
Typical result:
– PDO raw fetch: \~1 ms
– Eloquent → Model hydration: \~3–5 ms
3. Bulk Inserts
Why it’s slower:
Inserting many rows one-by-one with Eloquent invokes events, mass-assignment checks, and individual queries.
// core PHP bulk insert
$data = [];
for ($i = 0; $i < 1000; $i++) {
$data[] = "(?, ?)";
$bindings[] = "Title {$i}";
$bindings[] = "Body {$i}";
}
$sql = "INSERT INTO posts (title, body) VALUES " . implode(',', $data);
$start = microtime(true);
$pdo->prepare($sql)->execute($bindings);
$time = microtime(true) - $start;
error_log("PDO bulk insert: {$time}");
// Laravel Eloquent looped insert
$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
Post::create([
'title' => "Title {$i}",
'body' => "Body {$i}",
]);
}
$time = microtime(true) - $start;
Log::info("Eloquent 1000× create: {$time}");
Typical result:
– PDO single bulk statement: \~15–30 ms
– Eloquent in a loop: \~800–1200 ms
Tip: For large datasets in Laravel, use the query builder’s insert() with a batch array:
Post::insert($arrayOf1000Rows);
4. View Rendering
Why it’s slower:
Blade templates are compiled to PHP then rendered, and include features like sections, inheritance, and directives.
// core PHP simple include
$start = microtime(true);
extract(['name' => 'Alice']);
include 'views/hello.php'; // just: <?php echo "Hello, $name"; ?>
$time = microtime(true) - $start;
error_log("PHP include view: {$time}");
// Laravel Blade
// resources/views/hello.blade.php
// Hello, {{ $name }}
$start = microtime(true);
return view('hello', ['name' => 'Alice']);
$time = microtime(true) - $start;
Log::info("Blade render: {$time}");
Typical result:
– PHP include: \~0.2 ms
– Blade render: \~2–4 ms
5. Middleware Pipeline
Why it’s slower:
Each middleware class adds a function call and possibly logic (authentication, CSRF, logging, etc.) before reaching your controller.
// core PHP manual check
$start = microtime(true);
if (!isset($_SESSION['user'])) {
header('Location: /login');
exit;
}
echo 'Dashboard';
$time = microtime(true) - $start;
error_log("core PHP auth check + dispatch: {$time}");
// Laravel route with auth middleware
Route::middleware('auth')->get('/dashboard', function () {
return view('dashboard');
});
Typical result:
– Core PHP inline check: \~0.5 ms
– Laravel with one middleware layer: \~2–3 ms
– Add more (e.g. web, throttle, verified): each adds \~0.5–1 ms.
Summary
While Laravel’s extra milliseconds per request are negligible for most apps, they can add up under high throughput (hundreds or thousands of requests per second) or in tight loops (bulk data processing). If absolute raw speed is critical in a hot path, you can:
Bypass Eloquent for raw PDO or the query builder.
Cache compiled views and routes (php artisan config:cache, route:cache, view:cache).
Minimize middleware on speed-sensitive routes.
Use batch operations for inserts/updates.
Offload heavy tasks to queue jobs or background workers.
These optimizations let you retain Laravel’s developer ergonomics while mitigating its overhead where it matters most.