Laravel performance optimization

Tips for Laravel Performance Optimization

Spread the love

Since its release in 2011, Laravel has become popular choice for the development of business focused applications. One reason Laravel is too much popular in developers is its performance optimization that allows developers to fine tune Laravel based apps.

In this tutorial I will guide on how to optimize Laravel based application when in production.


For this purpose of tutorial, I assume you have installed Laravel on your server. I have following setup:

  • Laravel 5.5
  • PHP 7.1

Config Caching

Laravel provides outstanding command Artisan Cache Config which is very help full in boosting performance. The basic usage of command is as follows:

php artisan config:cache

Once you run this command, the changes you make will not have any effect. If you wish to refresh the cache, run this command once again. In order to clear the config cache, use the following command:

php artisan config:clear

To further optimize performance of your application you can use OpCache, that caches the php code, so you don’t have to recompile it again and again.

Routing Cache

Routes caching is very important optimization feature when you have lots of routes. The routes cache is a simple array and helps in speeding up Laravel Performance. Run the following command:

php artisan route:cache

Remember to run the command every time when you have made changes in routes file or added any new route. For clearing cache use following command:

php artisan route:clear

Remove Unused Service

In the context of Laravel optimization, an important tip is to not to load all services from the config, it is always important to disable unused services from config files.

Composer Optimize Autoload

It is a good idea to use Composer scan the application and create one-to-one association of the classes and files in the application. Use the following command:

composer dumpautoload -o

Limit Included Libraries

It is always important to review all the libraries that are called within the code. If you think you could do it without using a library remove it from config/app.php to speed up Laravel application.

JIT Compiler

On every request, converting PHP code to bytecode and then executing it is a resource intensive process. Therefore, go-between such as Zend Engine are required to execute the C subroutines. To reduce time, it is essential to repeat this process just once, this is where the Just-in-time (JIT) compiler has proven to be very useful. For Laravel based applications the recommended JIT compiler is HHVM by Facebook.

Cache Queries Results

By caching the results of the queries which are used frequently is a great way to optimize your Laravel based application. For this my recommendation is as follows:

$posts = Cache::remember('index.posts', 30, function()
{return Post::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();});

Use Eager Loading for Data

When Eloquent uses eager loading it loads all associated object models in response to the initial query. Let’s compare Eager loading and Lazy loading.  

The Lazy loading query will look as follows:

$books = App\Book::all();	
foreach ($books as $book) {
echo $book->author->name;}

The Eager loading query will look as follows:

$books = App\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;}

Precompile Assets

For Laravel tuning developers often distribute code in separate files. While this keeps the code clean and manageable, but it doesn’t contribute to efficient production. To help developers in this context Laravel provides simple commands:

php artisan optimize
php artisan config:cache
php artisan route:cache

Leave a Reply

Your email address will not be published. Required fields are marked *