Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vedoj's avatar
Level 1

Array vs non-array for performance of multiple middlewares

Given that mw1, mw2, and mw3 middleware do the same CPU work in the code, which code of the following 4 code examples will offer the best performance and why?

Code A:

Route::middleware(['mw1', 'mw2', 'mw3'])->group(function () {
    Route::get('/test', [TestController::class, 'index'])->name('test');
});

Code B:

Route::middleware(['mw1','mw2'])->group(function () {
    Route::middleware('mw3')->group(function () {
        Route::get('/test', [TestController::class, 'index'])->name('test');
    });
});

Code C:

Route::middleware('mw1')->group(function () {
    Route::middleware(['mw2', 'mw3'])->group(function () {
        Route::get('/test', [TestController::class, 'index'])->name('test');
    });
});

Code D:

Route::middleware('mw1')->group(function () {
    Route::middleware('mw2')->group(function () {
        Route::middleware('mw3')->group(function () {
            Route::get('/test', [TestController::class, 'index'])->name('test');
        });
    });
});
0 likes
7 replies
vedoj's avatar
Level 1

@vincent15000 I haven't asked about array vs non-array performance so far. I am asking because array performance in PHP is quite slow.

So, intuitively I would favor individual (onion layers) like calls because the arrays are slow in PHP.

So, I would say that Code D is the fastest and Code A is the slowest. But I am not a Laravel expert. That's why I am asking.

1 like
vincent15000's avatar

@vedoj That's not a Laravel problem, but a PHP one.

And Laravel debug bar displays the time needed to execute the different methods.

1 like
newbie360's avatar

CODE E: =(

class TestController extends Controller
{
    public function __construct()
    {
        $this->middleware('mv1:name')->only('store', 'update');
        $this->middleware('mv2:dob,dod')->only('store', 'update');
        $this->middleware('mv3:name,address')->only('store', 'update');
    }
2 likes
vedoj's avatar
Level 1

@newbie360 Thanks, good to know ;) Although, it's much more work and not so obvious to see everything in one view. Especially when I have multiple controllers in real apps. But again, thanks for your solution.

And what about the A..D options? As I said in previous comment, PHP is slow when dealing with arrays so my intuition would for this reason put code D as fastest and code A because of the most array operations as the slowest. Is this intuition correct?

1 like
newbie360's avatar

@vedoj i think you don't need worry about that, after the project almost finish,

lets review where should be make cache,

any query have N+1 problem,

any code should put outside of Loop,

database any column need for query but not indexed

any code can run in job

1 like
martinbean's avatar

@vedoj Sigh.

Remember in your last thread when I told you to profile instead of asking vague questions that only have vague answers?

2 likes

Please or to participate in this conversation.