DEV Community

Philip Perry
Philip Perry

Posted on • Edited on

2 2

3 tips for improving performance of a Laravel website

At the company I work for we tackled performance this last sprint. It had been noticed that the speed in production had become slow. There are of course many reasons for this and front-end also made some changes, but I'm just going to just write some things we did in the back-end.

To find the bottlenecks, we used the excellent tool called Clockwork https://underground.works/clockwork/ It uses a composer component to collect the data and to easily view it, you use a browser extension that runs in the dev tools. You could also use Telescope, but Clockwork is more powerful for profiling.

These are 3 of the things we did to improve performance:

1. Eager Loading

Using 'with' for loading related data, one can reduce the number of database queries made. Clockwork is helpful in that it displays how many models are created for a request. This article explains well how eager loading works.

2. Use indexes

Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. Use the EXPLAIN keyword in front of your sql statement to see how many rows are used for the query. Learn more here:
https://www.sitepoint.com/mysql-performance-indexes-explain/

3. Caching

Do you use the same long executing query more than once for a request? In that case save the query result in a registry. I haven't been able to find a website for this, but the basic idea is to use the singleton pattern to make sure you only load the object once and then you check if the data is already in the registry using a key for looking it up.

We also wrote some benchmark tests which will allow us to test in the future if a change has a negative impact on performance.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay