Table of Contents
The speed of a website is an important factor that determines its overall quality. It is estimated that if a website takes more than 3 seconds to load around 35% of people leave the website. So, the speed of a website is a very important factor.
Here are some of the techniques to speed up your Laravel websites and make them faster:
Remove Unnecessary Plugins and Packages
There may be some unnecessary dependencies that you might have in your composer.json file which might run every time a user makes a request. Check each dependencies and remove those that might not be necessary.
Let’s assume that an application has 15 Laravel packages. Each time a user makes a request those packages have 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 a heavy-sized package but only utilizing one or two features in that case consider writing the code yourself.
Consider Using the Latest Version of PHP
A new version means improvement in performance and fixed bugs. So why not use the latest version? This technique might be troublesome for those who are updating existing projects. Without taking proper safety measures, the whole project might crash. Having an automated test suite helps a lot while updating the PHP version.
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 the database in your application. You can do this by specifying only the columns that you need in the query.
Let’s 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 the required data. Imagine you need only the user’s 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 the Laravel website.
This method might not affect small scale. However, it can assist applications where load time is crucial.
Laravel Queues
Using Laravel queues is another way to improve your website’s performance. Sometimes there may be code that runs in the controller during requests that might not be necessary for web response. We can queue those codes 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 the above controller, the store() method stores contact form details in the database, sends an email to the address to inform about the new contact form submission, and returns a JSON response. In this approach, an email is sent before the user receives a response in the browser. Although it takes only a few seconds, there is always a chance that a visitor might leave.
To address this issue, we can take the 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 a response before sending mail. Once the response is returned, the process of sending mail will be executed. This process helps to get a response before an email is sent which speeds up our website.