Tips To Improve The Performance Of A Laravel Application

Hello dear web artisans, there are several ways you can improve the performance of laravel application. Recently I have developed an application using the laravel framework. Even though the possibility of having the performance issues while using this framework is quite less. But when developing complex applications developers may forget to focus on the performance related things while focusing on application requirements and its logic. So developers have to emphasize on the performance of the application also. Following small tips will greatly improve the performance of the application. Out of my learnings and after referring many resources I have written this tips which I also used practically to improve the performance of my laravel application.

List of Tips to improve the Laravel application performance:

  1. Config caching
  2. Routes caching
  3. Classmap optimization
  4. Optimizing the composer autoload
  5. JIT Compiler
  6. Choose a faster cache and session driver
  7. Cache the queries results
  8. Minimize Plugins
  9. Use “Eager Loading” of Your Data
  10. Precompile Assets  
  11. Use CDN for delivering your static assets.

 

1. Config caching

We know that the config is present across many files, and we have to include each of the files for every request. So combining all the files will be a good solution for this and you can do it using the following command.


php artisan config:cache

Once you cache the config, the changes you make do not have any effect. We can refresh the config by running the above command once again. In order to clear the config cache use the below command.


php artisan config:clear

2.Routes caching

The routes.php file can be cached by running the following command:


php artisan route:cache

When we use closures the artisan command throws an exception when we try to compile the routes, So it is recommended to move them into the controllers.

Just like the config cache, The changes you make in routes.php file will not have any effect once you cache it.  You can refresh the routes cache by running the above command again. If you want to clear the routes cache use the below command:


php artisan route:clear

3. Classmap optimization

A complex project will have numerous files, for every request laravel includes many different files. For this, it is recommended to declare all the files which are used for each request and combine them into one file. So that this file will be loaded for every request. This can be done using the following command:


php artisan optimize --force

4. Optimize composer autoload

The following command makes the composer scan the complete application and allows it to create one to one association of all the classes and files in the application


composer dumpautoload -o

This works for any application that uses the composer.

Once you have done the classmap optimization by running PHP artisan optimize, The command will also tell the composer to create an optimized autoload. So it is not needed to run the above command again.

5. JIT Compiler

We all know that PHP needs something like Zend engine to interpret the files and execute C routines. But this process takes time. For this, it is recommended to use JIT Compiler. This helps the application to act fast. Widely used JIT compiler for laravel is HHVM.

6. Choose a faster cache and session driver

To access the data faster, the best way is to store the cache and session entries in RAM, Out of all the cache and session drivers that laravel supports I recommend memcached.

For changing the session driver, The driver key can be found in the following file:


app/config/session.php

For changing the cache driver, The driver key can be found in the following file:


app/config/cache.php

7. Cache the queries results

Caching the results of the queries that are frequently run would be an another great way to improve the performance, For this, we use the remember function as follows:


$posts = Cache::remember('index.posts', 30, function()

{

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

});

8. Minimize Plugins

Laravel offers many plugins, and it is true that these plugins add more functionalities. And obviously, the number of libraries and files that has to be loaded also increases with this. This can slow down the things. So it is recommended to cut down the unnecessary plugins. We know that Laravel will use the composer to manage components, so removing the unwanted parts from composer.json file reduces the dependencies to be loaded.

9. Use “Eager Loading” of  Data

In order to map the object models to database tables, laravel use Eloquent ORM. But this uses a lazy loading approach which would affect the performance of the application. But with the Eager Loading, the queries will retrieve any associated object models as part of your initial query. Eager loading will reduce the N + 1 problems.

The lazy loading query will look like the following:


$books = App\Book::all();

foreach ($books as $book) {

echo $book->author->name;

}

The eager loading query will look like the following:


$books = App\Book::with('author')->get();

foreach ($books as $book) {

echo $book->author->name;

}

10. Precompile Assets

For keeping our code clean and maintainable we generally maintain separate files like configuration files and route files etc. But this only makes the code maintainable but does not profit in any way for production. So laravel provides us the following commands that combine this files into one.


php artisan optimize

php artisan config:cache

php artisan route:cache

Also for the CSS and JS fields, we had Elixir.I have already explained how to install and use of Elixir  here.

http://www.agiratech.com/laravel-development-how-to-force-a-browser-to-refresh-css-and-js-files-on-an-update/

11. Use CDN for delivering your static assets.

Loading the static assets files from CDN server will improve the performance of loading.

Conclusion :

So when developing complex applications, following the above points will definitely improve the performance of the application. To know more about Laravel web development follow Agiratechnologies.