Top 15 tips and tricks to Optimize Laravel Performance

Laravel is a great back-end framework for server side logic implementation. It has intuitive code structure, great documentation and very active community. Although the learning curve for Laravel is a bit steep, the payoff is also great. Laravel provides out of the box security tools for the application. With all that being said, while I was building my REST API using Laravel, I found the performance starting to slow down as the APIs grew larger.

So, I thought I’d do a post on how to optimize a Laravel back end system for maximum performance. In larger projects, performance is a key deciding factor in determining the success of the product. So, ensuring the performance of the Laravel application and optimizing the application down to the code level is a crucial skill that every developer should have. Since Laravel is used to build business products most often, the performance of Laravel powered applications has serious implications for the success of the business.

Here are the top 15 tips and tools to Optimize Laravel Performance:

Laravel Telescope

Laravel Telescope is an elegant debug assistant for the Laravel framework. Telescope provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps and more. Telescope is a must have monitoring and analysis tool for any apis built using Laravel.

Laravel Debugbar

Laravel Debugbar is a package to integrate PHP Debug Bar with Laravel. It includes a ServiceProvider to register the debugbar and attach it to the output. It is a package that can be used as a Laravel performance monitor. It is recommended to make use of this package while developing your application. Because with it, you can easily inspect how your application is running, and then improve accordingly.

Now, let’s move onto actual tips to tune the performance of your Laravel Application

Config Caching:

$ php artisan config:cache

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

$ php artisan config:clear

Also, you could use OPcache that caches the PHP code so you don’t need to recompile it.

Routes Caching

$ php artisan route:cache

Remember to run the command every time config or the routes files have been changed. Otherwise, Laravel will load old changes and from the cache. For clearing the cache, use the following command:

$ php artisan route:clear

Declutter the app from unnecessary services

Classmap Optimization

$ php artisan optimize --force

Composer Optimize Autoload

$ composer dump autoload -o

Reduce Redundant Dependencies

JIT Compiler

Cache and Session driver

Cache Queries Results

$articles=Cache::remember(’index.articles’, 30, function()

{

return Article::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();});

}

Use “Eager Loading”

The lazy loading query will look like:

$posts = App\Post::all(); foreach ($posts as $post)

{

echo $post->author->name;

}

In contrast, eager loading query will look like:

$posts = App\Post::with(’author’)->get();

foreach ($posts as $post) {

echo $post->author->name;

}

Precompile Assets

$ php artisan optimize

$ php artisan config:cache

$ php artisan route:cache

Use CDN for Static Assets

Assets Bundling and Minifying

Compiling all assets in a single place might end up with a huge size file. As a result, this practice will not allow our application to benefit from the proposed compilation. Therefore to resolve this issue, we can minify our assets using Laravel Mix.

$ npm run production

This will run all Mix tasks and make sure our assets are production-ready by minifying them. Once minified, the assets will become smaller in size, hence will be retrieved faster, which will speed up the performance of our application.

Conclusion

Original post: https://blog.ahmadwkhan.com/top-15-tips-and-tricks-to-optimize-laravel-performance