Published: August 02, 2024 by Lucas Rolff
How we cut a website's load times by 85% with a simple fix
Earlier this week, we posted about how we identify performance problems.
Today, we'll bring one of the recent cases to identify a performance problem for a website.
The website in question was recently migrated to our platform. Before the migration, we realized it performed slowly. This made us curious about what caused the slowness when loading the website since it appeared on every single page, even very basic or lightweight ones.
This often indicates something that's dragging the whole page down, and sometimes, it turns out to be super simple.
In this case, we set up profiling through CloudLinux X-Ray. This was done prior to performing the actual DNS migration for the customer while the customer was in the testing phase.
We performed a few requests to the various pages, and we consistently saw PHP response times of roughly 2.5-3 seconds every single time.
Looking at the system functions category, we saw this:
A call to the PHP function file_get_contents
, over and over again, always in the same template file default.php
; in this particular case, the call is being made on line 35. As you can see in the duration column, each call takes between 166 and 215 milliseconds.
Opening the file and going to line 35, we see the following:
In short, it iterates over an array and generates some HTML, which isn't unusual here. However, the file_get_contents
call a URL seems a bit weird indeed. A quick var_dump($icon)
reveals that the $icon['url']
is simply a URL to an SVG image on the page itself.
In this case, the result effectively inlines SVG images at the expense of making a remote call to the images for every single pageview within PHP.
We could solve this pretty easily by simply changing it to an img
tag as such:
Visiting the page confirmed that everything still functioned as it should, while the site suddenly loads in a couple of hundred milliseconds, which aligns with the speed we'd expect for such a website.
Finding the problem in this case took less than 10 minutes from start to finish, which resulted in the website being speeded up massively.
This website, in particular, generates only around 30.000 pageviews per month. While that's not a lot, even if we take 2-2.5 seconds off the page generation per pageview, we still save quite a few hours every month.
The result of 10 minutes of work is a happier customer and a much faster website, which hopefully results in increased conversions and an improvement in SEO, even if it's just a tiny bit.