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

acacha's avatar

How to improve Laravel views performance when using multiple times same view file or howto avoid repeating expensive read file operation

First of all I'm not asking about alternative ways of how to achieve the same result for example code proposed. I know there is better ways to do that.

The example code provided below is only for forcing Laravel to read multiple times the same view file (resources/views/hello.blade.php)

The example code:

Route::get('/testperformance3', function () {

    $faker = Faker\Factory::create();

    $ouput  = "";

    for ($i = 1; $i <= 5000; $i++) {
        $test = View::make('hello',["name" => $faker->firstName])->render();

        $ouput = $ouput . $test;
    }

    echo $ouput;
});

Where hello view is:

<div> Hello {{ $name }}</div>

The performance of this code is not very good because Laravel is executing multiple times and include operation for evaluating blade compiled view as you can see on evaluatePath method in PhpEngine.php file (https://github.com/illuminate/view/blob/master/Engines/PhpEngine.php)

protected function evaluatePath($__path, $__data)
    {
        $obLevel = ob_get_level();

        ob_start();

        extract($__data);

        // We'll evaluate the contents of the view inside a try/catch block so we can
        // flush out any stray output that might get out before an error occurs or
        // an exception is thrown. This prevents any partial views from leaking.
        try {
            include $__path;
        } catch (Exception $e) {
            $this->handleViewException($e, $obLevel);
        } catch (Throwable $e) {
            $this->handleViewException(new FatalThrowableError($e), $obLevel);
        }

        return ltrim(ob_get_clean());
    }

In my example case $data passed to view is changing every loop iteration (so I can't cache/save view result at first time). The expensive operation is at line:

 include $__path;

But in this example the path is the same over all loop iterations.

The question: Any idea how to improve Laravel to avoid repeating the same include operation again an again maybe caching the result when path is the same.

For whow it may concern I'm trying to improve Sleeping Owl admin performance because it's a case where a lot of view operation are made with the same view file when retrieving lists of items. Tou can see more infor at discuss:

https://laracasts.com/discuss/channels/laravel/how-to-improve-performance-when-calling-same-view-multiple-times

0 likes
2 replies
jekinney's avatar

Be interesting to see the performance marks with out faker. I know for a fact running PHP functions per cycle slows it down a lot. I have tested laravel form helpers in loops verses html. As I think I posted that in the other thread.

So if you are running not only blade and PHP but functions to while each waits for there turn it's going to be slow. Hence why you cache queries, ensure efficient queries, and fire events when a user doesn't need to wait (like email, processing images, etc).

While speed isn't a huge difference, passing objects versus arrays have proven to use up to 50% less memory which can be huge with 1000 users at once.

1 like

Please or to participate in this conversation.