10 Tips And Ideas for Laravel Performance

5 years ago4 min read510 views

Anamika Srivastava

Written by

Here you will get to know the different ways to enhance the performance of a Laravel application.

Here All Web Developers should follow all the below checkpoints before delivering any project in this framework.

1. Minify and combine CSS and javascript

We use javascript, CSS combine and minify. This allows to lower request from the website.

Package — https://github.com/eusonlito/laravel-Packer

This package help you to automatic minify all CSS and js file on production.

2. Use Caching

If you are using an eloquent model to interact with the database then you must use this below package to enable caching in the model. We use model caching for every dictionary (user type, contact status, etc..) loaded from the database. Caching model will also automatically invalidate itself after saving/updating of Eloquent object.

Cache model — https://github.com/GeneaLabs/laravel-model-caching

3. Use Redis or Database cache instead of file cache

https://laravel.com/docs/5.5/redis

4. Mod Expires

You must use this mod in htaccess file before going to live. Expires information. Caching Javascript, CSS, images and other assets in browser memory allows us to speed-up page load time.

<IfModule mod_expires.c>

AddType application/vnd.ms-fontobject .eot

AddType application/x-font-ttf .ttf

AddType application/x-font-opentype .otf

AddType application/x-font-woff .woff

AddType image/svg+xml .svg

ExpiresActive On

ExpiresByType image/jpg “access plus 1 month”

ExpiresByType image/jpeg “access plus 1 month”

ExpiresByType image/gif “access plus 1 month”

ExpiresByType image/png “access plus 1 month”

ExpiresByType text/css “access plus 1 month”

ExpiresByType application/pdf “access plus 1 month”

ExpiresByType text/javascript “access plus 1 month”

ExpiresByType application/javascript “access plus 1 month”

ExpiresByType application/x-javascript “access plus 1 month”

ExpiresByType application/x-shockwave-flash “access plus 1 month”

ExpiresByType image/x-icon “access plus 1 month”

ExpiresByType image/ico “access plus 1 month”

ExpiresByType text/css “access plus 1 month”

ExpiresByType text/css “now plus 1 month”

ExpiresByType application/vnd.ms-fontobject “access plus 1 month”

ExpiresByType application/x-font-ttf “access plus 1 month”

ExpiresByType application/x-font-opentype “access plus 1 month”

ExpiresByType application/x-font-woff “access plus 1 month”

ExpiresByType image/svg+xml “access plus 1 month”

ExpiresByType text/html “access plus 600 seconds”

ExpiresDefault “access plus 2 days”

</IfModule>

5. If You are still using PHP 5 — Change it to PHP 7

You need to upgrade your PHP version on your server. It will enhance the performance of the website.

6. You must use “EAGER LOADING” OF YOUR DATA 

When you invoke queries via Eloquent, it uses “lazy loading” approach.

$books = App\Book::all(); // 20 books in table

foreach($books as $book){

echo $book->author->name;

}

For every book in database eloquent will run separate SQL query (21 queries instead of 1).

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

foreach($books as $book){

echo $book->author->name;

}

Eager load query will run only one SQL query with JOIN.

7. Minimize Use of Laravel Plugins

There are many plugins for Laravel that allow you to easily add more functionality. Remove from composer plugins that You are not using.

8. Use artisan commands

Laravel comes with commands for boost performance.

php artisan config:cache

php artisan route:cache

php artisan optimize — force

Commands are useful when you are using lots of routes and configuration files.

Tip: Don’t forget to clear the cache after changes.

php artisan config:clear

php artisan route:cache

php artisan view:clear

9. Use New Relic

This package will help you to check memory usage and queries. You can get more information from the given link below.

https://newrelic.com/php/laravel

10. Use pusher or some other similar library for real-time interaction

You can use this to develop some real-time interaction between the user to the server. Like messaging app or application with notification feature.