Tips to Improve the Performance of Your Laravel Application

Daud Samim
7 min readNov 30, 2021

--

In this blog, I have covered my experience of using Laravel and overcoming the performance laybacks. We are here with this ultimate Laravel performance guide to go easy on your disillusioned mind and waver all the dark clouds of poor Laravel app performance. Read in-depth to learn about the best 11 optimization tips to improve the performance of the Laravel applications.

Introduction

I am working on a pretty heavy Laravel website with a huge number of requests and structured query language/eloquent calls. There is high-CPU VPS and high-memory still I feel that there is a lot of room for performance improvement. Laravel has become the first and foremost choice for building business-focused applications including eCommerce platforms and information management systems.

I agree, Laravel has grown by leaps and bounds and its consistent finetune performance has again made it the best PHP framework for 2018. From simple web applications to complex APIs Laravel is a one-stop PHP development framework. Quick development approach, MVC architectural pattern, and a set of libraries make it so convenient to build well-structured beautiful code.

Ever since Laravel was released in 2011, it has gradually flourished into multiple folds, and currently, developers consider Laravel as the best PHP framework for enterprise applications.

Seasoned Laravel developers believe that Laravel is the only reason that PHP has retained itself in the markets. If it weren’t for the Laravel framework, they might have moved over from PHP long back.

The framework has some outstanding functionalities that have made the life of developers easy-peasy. As you have decided to use Laravel for your upcoming projects, you must learn about the tactics and tricks to use it to this fantastic framework’s maximum potential. This is the best part of using Laravel- that it enables you to fine-tune or optimize your website for enhancing its performance.

Why Focus on Laravel Performance Optimization?

You have surely created a Laravel application where the Laravel developers have used the PHP framework and essential libraries to bring out the maximum potential of your project idea. But, there’s always room for perfection and enhancements.

Mostly, businesses use Laravel to build their business information system, and hence the performance of their business is critically related to the performance of Laravel. To ensure smooth performance and deployment of your Laravel project, you should prioritize optimizing the Laravel application for your business benefits.

The Laravel management information system enables you to take vital business decisions, and hence optimizing the performance of the Laravel app will surely enable improved business prospects.

Optimization Tips to Improve performance of Laravel application

1. Routes caching

If your Laravel application has a lot many routes and configurations, then the PHP artisan route cache is an essential feature to speed up your Php app improvement.

Run the listed below command to cache the routes.php file

php artisan route:cache

At the time of using closures, the artisan command hurls an exclusion when we even try to compile the routes it is suggested to interchange into the controllers.

It is similar to the config cache, the changes you amend in routes.php will not have a favorable effect once you cache it. If you want to refresh the routes cache, you can run the above code again. But if you want to clear the routes cache run the below code.

php artisan route:clear

2. Use Artisan Commands Effectively

Laravel comes with extra perks when it comes to boosting performance

1. php artisan config:cache
2.
3. php artisan route:cache
4.
5. // Note: Artisan Optimize was removed in laravel 5.5 this works in previous versions.
6. php artisan optimize --force

Commands are beneficial when you are using so many configuration and routes files. Tip: Make sure to clear cache after changes.

1. php artisan config:clear
2. php artisan route:cache
3. php artisan view:clear

3. Use Deployment tool to Appeal All Commands

I know, it cannot be considered as a performance tip, but it will help you reduce your time, and that matters. A deployer is a deployment tool and if you have ever used composer to handle your project dependencies, you’ll feel so right. Deployer can be deployed to Laravel application as its seeds, optimization, and migrations with one command.

php deployer.phar deploy production

Check out the article implementing deployer, How to deploy Laravel

4. Eager Loading

The most common issue at the time of retrieving eloquent relationship is N+1 query problem. Let me help you understand this scenario with two different models’ flats and their owners. Consider if you want to retrieve their owners and to achieve that, you will be required to run the following code:

$cars = App\Flat::all();

foreach ($flats as $flat) {
echo $flat->owner->name;
}

It will help you execute one query to find out all of the flats from the database, and another query will help you find out the owners. Let’s move forward, we have 100 flats and this loop requires 101 queries: one for the flat and additional to find out the owner of each car. Does it sound so common because we are using a small database. But, I want you to imagine a large dataset and queries to imagine the actual picture.

To overcome this problem, I prefer to use eager loading.

$flats = App\Flat::with('owner')->get();

foreach ($flats as $flat) {
echo $flat->owner->name;
}

Running the above code, you can reduce the operation in just two queries.

5. Queues

How about using Queues to improve the performance of the application. The image you have developed is an application that sends a welcome email to new users upon sign up. It makes use of third-party email services such as Mailgun, when users signup the form a new user record will be inserted into the database, so a call is redirected to the Maligun to send a welcome email.

This third-party email may take a few seconds so the user might think that the sign-up process is slow at this time you can use Queue to send an email and run it as a background task. The code might be lookalike below:

public function register(Request $request)
{
// validate form inputs
$this->validator($request->all())->validate();

// persist user to database
$user = $this->create($request->all());

// send welcome email
Mail::to($user)->send(new WelcomeEmail($user));

// log user in
$this->guard()->login($user);

return $this->registered($request, $user) ?: redirect($this->redirectPath());
}

Running this code will not only improve the performance of the application but will also enhance the user experience.

6. Leverage JIT Compiler

PHP is a computer machine and server-side language it does not comprehend PHP code natively. Usually, the programmers make use of a compiler to compile the code into bytecode and interpret the PHP code. The program compilation procedure affects Laravel application performance as well as the user experience. SO, Laravel programmers can make use of Zend Engine that comes with the just-in-time compiler to compile the code quickly and once.

7. Compress Images

If your Laravel application contains lots of images then you should compress all of them to optimize the performance. There is a number of ways to do the optimization. However, different types of images require different tools to maintain the quality as well as the resolution of images.

If you are making use of Laravel Mix, then it is advisable to use an NPM package like ImageMin while compiling your images. For a very large image, try TinyPNG to compress the image first and then use ImageMin to compress it as much as possible.

8. Classmap optimization

A smart trick to keep your Laravel app compact and ready for easy loading should keep all your inclusive files together. As you want to include multiple files in your application, you just need to call and have one common file.

Such a move will fasten your Laravel application by including several files in a combined file for loading into your application.

9. Precompile Assets

Developers will always maintain the right development environment by keeping different assets needed in your application separate. Though this is a good practice, it is not mandatory for production scenarios. And sure, Laravel has in-build artisan commands that can help you with this bifurcation.

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

By using these above commands, Laravel will automatically classify your most used classes and keep it in a single file.

10. Assets Bundling

All Laravel applications are accompanied by the instrumental Laravel Mix, which effectively builds your application API from Webpack. Laravel Mix consists of common CSS and Javascript preprocessors that offer you script, styles, and other compilations.

For example, if you want some set of specific style formats for your application file, like:

mix.styles([
'public/css/vendor/normalize.css',
'public/css/styles.css'
], 'public/css/all.css');

By using Laravel Mix, your application will auto-create an all.css file from normalize.css and style.css. Hence, you can merge both the style sheets within a single all.css file instead of retrieving them individually. This hack will definitely improve Laravel application performance.

Check out this interesting video on Laravel Performance Optimization Tips.

11. Assets minifying

As you implement the above performance optimization tip by compiling assets in one file, what happens is your loading file turns out to be huge. To overcome this issue, you may use the following command:

$ npm run production

Conclusion

We hope you liked this articulation, and it will definitely turn to be fruitful in your Laravel app development. You must surely take a Laravel performance analysis and find out yourself. To improve the performance of the Laravel application, feel free to reach me on my LinkedIn account & email address.

--

--

Daud Samim
Daud Samim

Written by Daud Samim

A highly energetic software engineer with 4+ years of experience in developing robust code for high volume businesses and good knowledge of software technique