Tips to optimize Laravel performance

Tram Ho

Config Caching

Laravel provides an interesting Artisan command, with the Artisan Cache Config being very helpful for improving performance:

Once the cache is configured, your changes will not take effect. If you want to refresh the configuration, run the above command again. To clear the cache configuration, use the following command:

Routes Caching

Route caching is an essential feature to optimize, especially for applications with multiple routes and configurations. The route cache is a simple array that should help increase Laravel performance, using the following command to use the route cache:

Remember to run the command every time the config or route files are changed. If not, Laravel will load the old code from the cache. To clear the cache, run the following command:

Remove Unused Service

An important tip is that you should not load all services through config, disable the unused services in the config file, add comments to service providers.

Classmap Optimization

A simple tip is to declare the included files to combine them into one file, thus, for all include requests, only one file will call and load, using the following command:

Composer Optimize Autoload

To increase the optimization we can use composer to scan the application and create one-to-one association class and file in the application, use the following command:

Limit Included Libraries

Using too many libraries will reduce the speed of the application, the user experience is reduced, the server must pay more data. If any library is not in use it is a good idea to remove it in config/app.php to speed up Laravel, another important place to look is composer.json

JIT (Just-in-Time) Compiler

Translating PHP code into bytes then executing it is a resource-intensive process. That is why Zend Engine is required to execute C subroutine. This process must be repeated each time the application is executed. To reduce time, it is necessary that this process be repeated only once. This is where the Just-in-Time compiler (JIT) proved to be very useful. For Laravel applications, the recommended JIT compiler is Facebook’s HHVM .

Choose a Fast Cache and Session driver

To optimize performance, it is best to store caches and sessions in RAM, such as Memcached . You can change it in app/config/session.php and app/config/cache.php .

Cache Queries Results

Caching the results of frequently called queries is a great way to reduce queries, increase performance:

Use “Eager Loading” for Data

n+1 increases the number of queries, reduces performance, wastes resources and time, there are many articles talking about this issue, I will not explain more. To use lazy loading you can use the following:

Precompile Assets

To adjust Laravel, developers often put code into separate files. It keeps the code clean and manageable, but it doesn’t increase application performance, Laravel provides a mechanism to improve it using the command:

Use CDN for Delivering Static Assets

  • Speed ​​up website access, load content quickly, minimize latency and jerks.
  • Significant bandwidth savings for static data (images, css, javascript).
  • Allows Internet users to interact quickly, increase satisfaction when accessing the website in real time.

Bundling Assets

Laravel Mix will compile assets into specified files, which you can customize in webpack.mix.js . For example:

All styles will be built into the file public/css/all.css .

Assifying minifying

Minify all assets using Laravel Mix, reduce size, get data faster, speed up the application, run the following command:

Running the latest version of PHP

The latest version of PHP has brought significant improvements in its performance. Therefore, make sure your Laravel application is running the latest version of PHP, so you can enjoy all the performance improvements introduced in the new version of your application.

Laravel Debugbar

This is a very useful package for development process, you can easily inspect how the application is run and then improve accordingly.

General Performance Tuning Tips for Laravel

Laravel page speed composer package

Download renatomarinho / laravel-page-speed package using composer, just add package to composer.json and run composer update :

Update Provider Details

After extracting the package, go to config/app.php and add the service provider:

Publish the package

After adding the package, we need to publish it:

Adding middleware for web access

After publishing the package, we will add the middleware in Kernel.php :

Check in route:

View page details

Finally create the blade file and enjoy.

summary

Above are useful tips to improve Laravel performance, thank you for reading here. Happy coding! ❤️

Share the news now

Source : Viblo