Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Speed Up and Optimize The Performance Of Laravel Application

If we say that Laravel is the first choice of the PHP developer, residing in any part of the world, it is not an overstatement. Just check out all online forums and discussion groups for PHP communities and you will find a common voice amongst them- Laravel has revitalised the way PHP development is done. It is the first choice of PHP developers for many years due to the unparalleled advantages it offers to the developers. Furthermore, a large Laravel community also makes it easier for developers to solve doubts and queries.

“Though there is a problem with Laravel. Take any Laravel application and you will find that the Laravel application performance is not up to the mark.”

What is the main reason for such a low performance and speed of Laravel application? It is the tons of work happening on the backside that makes it sure that you have a good time as a developer. Whenever you make a feature run on Laravel, it has to work on layers of code to be whipped up to execute the command. It is the main reason why Laravel application is slow compared to other applications made of different programming languages.

In a cut-throat competition era where users are restless and with multiple options at their disposal to choose from, they expect speed and smooth performance of any web apps they are using. If you fail to offer that, they will simply switch to your rivals. It is not a condition that you want to face off, do you? It is very crucial to take some measures for optimization of Laravel application and it is possible too. All you need to do is to read this blog carefully till it ends and gain some insight and information about how to improve the speed and performance of Laravel application.

Let’s get started.

First of all, let’s see how to optimize your Laravel app for 100/100 page speed score.

Google PageSpeed insights are very crucial to determine the speed and performance of web applications. Significantly, your app scores are high when the PageSpeed is measured. The process is split into four small elements.

  • Images
  • Scripts
  • Styles
  • Server Speed

1. Images
Optimizing images is a very important thing to do if you want to improve the speed of the Laravel web application. There might be static images or images that are part of the design of the web application. There are different tools to be used for different images for image optimization. Some of the tools help for image optimization is

  • PNGcrush
  • Gifsicle
  • Joegoptim
yum install -y libjpeg-turbo-devel

./configure
make
make strip
make install

Now, let’s see how images are optimized for better speed and performance of a Laravel application.

These are some scripts that can be used for image optimization.

For PNG photos

find .-type f -name "*.png"-printf'%p\n'-exec pngcrush-ow -q -reduce -brute "{}" \;

For JPG photos

find .-type f \(-name "*.jpg"-o -name "*.jpeg" \)-exec jpegoptim--strip-all --all-progressive "{}" \;

Also, you can use a framework called Laravel-Image-Optimizer, a very superior image optimization tool for Laravel.

2. Scripts

When you use Laravel, you are automatically using Elixir and Mix, a script merging and minimizing tools. However, when it comes to external scripts such as Google Analytics scripts or MailChimp scripts, we need to download and merge the scripts with the internal scripts. It is easy to manage one file that has internal and external scripts.

3. Styles

When it comes to styles, you need to ensure that you end up minimizing them too. However, it is a hard task and you need to ensure that you come up with styles that are easy to load while rendering the main page.

4. Server Speed

If you are using Apache HTTP server, you can improve the performance of the app by picking from HTML5 Boilerplate Source Code and move them to your Apache config.

For application response time, you need to check out the slow queries or external services that your app is using such as external API calls and mail sending facilities.

When it comes to minifying output HTML, create a middleware that improves the response time and minify HTML output.

A good example is shown in the image below.

<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Response; classMinifyHtml { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ publicfunctionhandle($request, Closure $next) { /** * @var $response Response */ $response=$next($request); $contentType=$response->headers->get('Content-Type');
if(strpos($contentType,'text/html')!==false){
$response->setContent($this->minify($response->getContent()));
}

return$response;

}

publicfunctionminify($input)
{
$search=[
'/\>\s+/s',// strip whitespaces after tags, except space
'/\s+</s',// strip whitespaces before tags, except space ]; $replace=[ '> ',
' <',
];

returnpreg_replace($search,$replace,$input);
}
}

How to compress images and reduce image sizes?

Yes, with a Laravel web application, it is important to compress images to improve the speed and performance of the application. There are different code snippets and methods by which you can surely compress your Laravel application images. It is obvious that large images take more time to load the page and it is a frustrating thing for users to wait for more than a few seconds to load the page.

Google came up with an excellent tool called Squoosh app for image compression and size reduction. Most of the Laravel developers use this app only. However, there are some other tools available for the job also such as Tinyjpg.

Also, there is the Laravel Image Optimizer by Spatie that can optimize different types of images such as PNGs, JPGs, SVGs and GIFs by running these images through a chain of various tools for image optimization.

Laravel speed test with PingDom

No matter how attractive, feature-rich and functional your website is, if it is not quick at loading and performing superiorly when opened, there is nothing to gain there. Your website will not serve the core purpose for which you have built it in the first place. If you are hiring a Laravel application development company, you need to check out the speed of the websites they have built previously and you will come to know about the competence and expertise the company possesses.

In most of the Laravel web application development projects, developers give more emphasis on UI and UX rather than speed of the webpage. I am not saying that UI and UX are not important, but so is speed. Do you like waiting for minutes for a page to reload when you open any website? Well, no one likes.

If the website’s loading speed is not satisfactory, you might notice a high bounce rate and smaller session time. Even your efforts for SEO will go in vain if your website’s speed is not good. Not just SEO rankings, you will end up losing your loyal customers who prefer speed over anything else. Hence, it is of utmost importance that you check your website speed and keep checking and optimizing it from time time.

Some tips for speed optimization of the Laravel website

First and foremost, you need to rework on your web design. For example, Google Maps or some other 3rd party tools might generate some errors that in turn, decide your speed. Also, heavy CSS and JavaScript needed to be refactored to make them smaller.

The best way is to use a template that is well coded and does not use many plugins.

Configure web server
Your web server needs to be configured properly to improve the speed of the web application. What a web server does? It controls gzip and cache settings that are required for a high ranking score. What to do for web server configuration?

For caching, enable caching on JS/CSS/Images for at least 7 days.
For gziping, you need to gziped all your files to improve the page speed score. Hire Laravel developers from a leading Laravel development company who have in-depth knowledge and experience of configuring web servers. Ask them about it while taking their interviews.

Laravel configuration
For Laravel application configuration, you need to do the following things.

Minify CSS/JS
It is very easy and can be done by using tools such as Elixir. You can also do it by manually minifying and combining your CSS/JS.

Minify HTML
There are different packages available for minifying HTML automatically.

Cache 3rd party resources
The main issue arising here is that Google Analytics sets a low cache time and Page Speed Insights requires a long cache time. You need to catch the script locally temporary to improve the page speed.

Template putting up into Laravel
Once you have selected the template for the website, you need to put it in Laravel. First of all, you need to copy assets.

Copy the templates js/css asset folders into resources/assets/ and then copy the templates fonts folder into public/. Then you need to download third party resources such as jQuery and bootstrap into your resources/fonts folders.

elixir(function(mix) {
mix.styles([
        'bootstrap.css',
        'clean-blog.css',
        'font-awesome.css',
    ], 'public/assets/css');
mix.scripts([
        'jquery.min.js',
        'bootstrap.min.js',
        'clean-blog.js',
    ], 'public/assets/js/');
});

With gulpfile.js creation, combine all the assets and combine them into 1 script. Now, you have minified CSS and js that can be updated with the code.

HTML updation
To update HTML, you need to remove all CSS link tags combined into all.css and then replace it with the following.


Then you need to replace all the


CSS optimization
Keeping CSS minimal and clean is essential and hence, you need to remove all the comments and other stuff that are of no use.

Image optimization
If you use CMS, install some plugins to optimize images or you can go to compresser.io and optimize all the images.

Fonts optimization
It is also important to improve the speed of the website and it can be done by hosting all fonts locally or by downloading Google Fonts and update the CSS accordingly.

How to improve Laravel performance?

Minimize plugins
One of the main parameters that slow down the loading speed of Laravel application is using too many plugins for different functionalities and features. There is a large number of plugins available for Laravel web application and with these plugins, more libraries and files come that take too much time for loading. You need to get rid of unnecessary plugins that you are using to improve the Laravel web application performance and speed.

Use eager Loading of your data
For easy and smooth CRUD (create, retrieve, update, delete) operations, Laravel uses Eloquent ERM supports mapping of your object models to the database tables supporting it and handles database interaction. However, Eloquent uses a lazy loading approach that asks a special query and reference in the code for data retrieval. It might result in a lot of queries and might reduce performance.

Eager Loading aspect is much more convenient and easy. By making queries to use eager Loading, they will retrieve any associated object models and will be available to use at any given time.

See below image for more information.

Lazy Loading approach

$books = App\Book::all();
foreach ($books as $book) {
echo $book->author->name;
}

Eager Loading Approach

$books = App\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;
}

Use deployment tool for command invoke

Well, we cannot tell that it will improve the performance of the Laravel application, but it will reduce the loading time. A Deployer is a fantastic tool for PHP that can be used to invoke all commands and to deploy application’s Migrations, seedes, optimization commands with a single command.

Use caching
Use the Laravel model caching to cache anything that you can for each dictionary. Once it has saved and updated an eloquent object, it will automatically invalidate itself. Even you can cache full response if your website is static.

Precompile assets
It is important to have all your assets in separate files during the development stage, but not for production. So, before deployment, there are few artisan commands that you can use for this purpose.

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

It will help you to compile all the classes that you use frequently into a single file and also help you to combine your routes and configuration files into single files. It will lead to faster loading website and that’s what you are looking for.

Conclusion

When it comes to improving the speed and performance of Laravel application, there are so many possibilities available for you. All you need to do is to carefully evaluate all the possibilities and follow Laravel performance tips described in this post. Surely, it will lead to faster loading website with superior performance and smooth navigation.

Connect With Us Today For
Speed and Performance optimization Of Your Laravel Application

[contact-form-7]


This post first appeared on I-VERVE TECHNOLOGY, please read the originial post: here

Share the post

Speed Up and Optimize The Performance Of Laravel Application

×

Subscribe to I-verve Technology

Get updates delivered right to your inbox!

Thank you for your subscription

×