Performance Improvement in Laravel App

Performance Improvement in Laravel App

Learn the Ten Best Ways to Improve App Performance in Laravel

Performance is the key factor for the success of any website. Laravel is the most popular framework amongst all PHP developers that helps improve app performance. It comes with a lot of inbuilt features that an application needs.

From simple web applications to complex APIs, Laravel uses the MVC architectural pattern, inbuilt libraries, easy connection with the database, and many more. One of the most important things for a website is its performance, therefore there are multiple ways to optimize and improve it.

Let’s discuss some of the tips to improve the performance of the Laravel application. We will be discussing both front-end and back-end optimization.

1. Use Caching

Some of the data retrieval or processing tasks performed by your application can be CPU intensive or take several seconds to complete. In such cases, it is common to cache the retrieved data for a time so it can be retrieved quickly on subsequent requests for the same. The cached data is usually stored in a quick data store such as Memcached or Redis.

We can put the data in the cache for it to be retrieved quickly. When we have many routes and configurations, we can use php artisan route: cache to cache the route.php file and access it easily. We can also easily refresh the route cache with php artisan route: clear.

We can keep a key-value pair in the cache and can refresh it whenever required.

Cache::put('key', 'value', $seconds = 100);

We can easily retrieve the key by using

$value = Cache::get('key');

2. Lazy Loading of Images on the Website

Lazy loading images refers to loading images on websites asynchronously. Images with higher sizes take more time to load, and lazy loading of images will improve the application performance.

<img src="myimage.jpg" loading="lazy" alt="..." />
<iframe src="content.html" loading="lazy"></iframe>

3. Reduce the CSS Size

A CSS file is a blocking resource, the browser has to wait for the CSS file to be downloaded before loading the page. This means that the longer the CSS file will take to download, the longer the page takes to load. So If we have a smaller CSS file, the page will load faster. The best way to reduce the file size is to use reusable CSS classes and remove all the classes which are not required.

There are various third-party tools available to reduce the CSS file size. We can use them to reduce the downloading time of CSS on the website and the page can be loaded quickly.

4. Use Queues

While building our web application, some tasks, such as parsing and storing an uploaded CSV file, can take too long to perform during a typical web request. Thankfully, Laravel allows us to easily create queued jobs that may be processed in the background. By moving time-intensive tasks to a queue, your application can respond to web requests with blazing speed and provide a better user experience to the customers.

So, if we have some jobs which can be done in the background, we can push those in the queue to optimize the application performance with a quicker response time. Queues can perform all the tasks in the background. It is one of the best features of Laravel since it increases the application response time.

5. Compress Images

If our application consists of multiple images, then it's better to compress all the images. Compressed images will take less time to load and will increase the application performance. We have many packages available to compress the images such as ImageMin, TinyPng, and many more. We can make use of these packages and improve the loading time of the application.

6. Fetch Only the Required Data, Not All

While querying from a database, we can fetch only the required data, instead of all the data. Fetching only the required data will take less time to load and will increase the application performance.

Suppose we want only name, email, and joining date from a User model. Then in place of,

$user = User::get();

We can use

$user = User::select(“name”, “email”, “joining_date”)->get();

7. Use with While Fetching Relational Data

While using the eloquent relationship to fetch the relational data, from a different table, instead of calling the relational model repetitively, we can fetch all the data at once. It will reduce the loading time of the page and improve performance.

For example, we have User Modal and Reporting Manager Modal and each user has one reporting manager. In place of the following code:

$users = User::get();
foreach($users as $user) {
    echo $user->reportingManager->name;
}

We can use this code:

$users = User::with(‘reportingManager’)->get();
foreach($users as $user) {
    echo $user->reportingManager->name;
}

And it will take comparatively less time than the previous one.

8. Use Indexing in the Database

Using indexing will index that particular column and it helps execute the query faster. We generally use the indexing on the primary key, however, it can be used in other searchable columns too. Indexing improves the query process time which results in improved performance.

Schema::create('reporting_manager’', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->integer('user_id')->index();
           $table->string(‘name’);
           $table->timestamps();
       });

9. Precompile Assets

Developers often distribute their code in multiple files to keep the code clean and manageable. Though it’s a good practice, it’s non-mandatory for production scenarios to optimize performance. We can use the below artisan command to improve the performance of the application.

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

10.Assets Bundling

Laravel Mix is a package that provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript preprocessors.

In other words, Mix makes it a cinch to compile and minify your application's CSS and JavaScript files. Through simple method chaining, you can fluently define your asset pipeline. For example:

mix.js('resources/js/app.js', 'public/js')
   .postCss('resources/css/app.css', 'public/css');

Mixing will improve the application performance.

Conclusion:

All the above tips help improve the performance of Laravel apps. Implement them and observe how they make a difference in your app.