Tips to speed up your laravel websites

Speed of a website is an important factor that determines its overall quality. In fact, it is estimated that if a website takes more than 3/4 seconds to load around 35% of people leave the website. So, speed of website is a very important factor.

Here are some of the techniques that laravel developers use to optimize website and make it faster:

1. Remove unnecessary plugins and packages

There maybe some unnecessary dependencies that you might have in your composer.json file which might run every time a user makes request. Check each dependencies and remove those that might not be necessary.

Lets assume that an application has 15 laravel packages. Each time a user makes request those packages has to be instantiated and run which automatically slows down the application. This may not have much effect on websites that have small traffic but a noticeable difference can be seen in large applications.

So be sure to remove unnecessary packages for higher efficiency. Also, if you are using heavy sized package but only utilizing only one or two features in that case consider to write the code yourself.

2. Consider Using Latest Version of PHP

New version means improvement in performance and fixed bugs. So why not use latest version? This technique might be troublesome for whose who are updating existing projects. Without taking proper safety measures, the whole project might crash. Having automated test suite helps a lot while updating PHP version.

3. Only Fetch Fields that you need

This might be the easiest way to speed up your website. It helps to reduce unnecessary incoming data from database in your application. You can do this by specifying only the columns that you need in the query.

Lets imagine we are trying to get customers data and there are about 1000 users. Some of your code might look like this:

$customers = Customer::all();

foreach($customers as $customer) {
    // Do something here
}

If there are about 10 fields in your Customer model. The above query would have to retrieve 10000 fields worth of data. We can reduce incoming data by specifying only required data. Imagine you need only the users’ id and email. If we specify these two columns in the query we can reduce incoming data up to 2000. By using this method, we can reduce the network traffic and amount of data which helps to speed up laravel website.

This method might not have effect on small scale. However, it can assist applications where load time is crucial.

4. Laravel Queues

Using laravel queues is another way to improve your website’s performance. Sometimes there may be code that runs in controller during request that might not be necessary for web response. We can queue those code to optimize our website.

Here is a simple example to make this concept clearer:

class ContactController extends Controller
{
    /**
     * Store a new podcast.
     *
     * @param  Request  $request
     * @return JsonResponse
     */
    public function store(ContactFormRequest $request)
    {
        $request->storeContactFormDetails();
        Mail::to('[email protected]')->send(new ContactFormSubmission);

        return response()->json(['success' => true]);
    }
}

In above controller, store() method stores contact form details in database, sends email to address to inform about new contact form submission and return JSON response. In this approach, email is sent before the user receives response in browser. Although it takes only few seconds, there is always a chance that a visitor might leave.

For address this issue, we can take this approach below:

class ContactController extends Controller
{
    /**
     * Store a new podcast.
     *
     * @param  Request  $request
     * @return JsonResponse
     */
    public function store(ContactFormRequest $request)
    {
        $request->storeContactFormDetails();

        dispatch(function () {
             Mail::to('[email protected]')->send(new ContactFormSubmission);
         })->afterResponse();

        return response()->json(['success' => true]);
    }
}

In this approach, we have instructed laravel to return response before sending mail. Once the response is returned, process of sending mail will be executed. This process helps to get response before email is sent which speeds up our website.

Leave a Comment