Last Updated:

Laravel Performance Optimization

Laravel Performance Optimization

Hello. In this article we will talk about Laravel, about optimizing a web application on Laravel. Yes, Laravel is simple, fast, but like any web application can be further improved (optimized performance). And increasing the performance of Laravel is quite simple. To do this, you must follow some rules.

 

And so, Laravel allows us to create powerful code with minimal effort. But like all web applications, Laravel applications require performance optimization. Performance is one of the key factors in the success of a line-of-business application. Let's look at the basic rules for optimizing an application that uses Laravel.

Configuration caching.

Laravel is able to cache the application configuration. To do this, use the artisan command:

php artisan config:cache

But be careful, the configuration changes you make will not work until you reset the configuration cache:

php artisan config:clear

Route caching.

Laravel allows you to cache and route, which greatly speeds up the application. To do this, use the artisan command:

php artisan route:cache

But be careful, when changing the ruts, it is necessary to clear the cache:

php artisan route:clear

Delete unused services

You need to disable unused services in the configuration file. Just comment them out in the config/app file.php

Class Optimization

Like any web application (more or less serious), it has several file inclusions using the include construct. Yes, this increases the readability of the code, but it slows down the performance of the application itself. To remove the inclusions and create a single file, run the following command:

php artisan optimize –force

Optimize startup with composer

In order to optimize startup, use the following command:

composer dumpautoload –o

Deleting and Optimizing Libraries

Laravel is very popular due to the large number of libraries that increase the functionality of the application. But not all libraries are of high quality. So if you don't need a library don't use it. To do this, remove it from the config/app file.php

Using the JIT Compiler

Translating php-code into bytecode is quite a resource-intensive process. Therefore, it is recommended to use a JIT compiler in production. For Laravel applications, the recommended JIT compiler is Facebook's HHVM.

Using the Quick Cache

To optimize Laravel performance, you must use a cache for frequently used and infrequently changed data, as well as for sessions. One of the best options is to use Memcached.

Using greedy and lazy loading.

Is everyone aware of the N+1 issue of the request? No, then you're here. Use greedy or lazy loading. In the article "Greedy and lazy loading in Laravel. Methods with() and load()." describes what is the difference and what is the advantage of them.

Conclusion.

We have considered some ways to optimize the performance of the Laravel application, which are already included in the framework itself. You can also advise to use a CDN for static content, use nginx, optimize (compress) images that are used on the site, etc.