Home » Magento tutorial for developer » How to reduce time to first byte (TTFB) and speed up Magento

How to reduce time to first byte (TTFB) and speed up Magento

Magento is the great framework for e-commerce websites. It has many extended features but it might be slow. You might be frustrated when you realize that your newly created webshop is not as fast as you would like it to be. In this article I will show you how to make magento faster by optimizing code and reducing time to first byte (TTFB).

In many cases poor magento performance comes from poor coding. You theme files might have php code blocks that run unneeded model calls or do something that could be done magento way. But first we need to identify those parts of magento code that are slow. For that we will need to enable Magento Profiler – the built in magento tool that lets you see what magento is doing and on what it spends most of the page load time.

How to enable Magento Profiler

In order to enable Magento Profiler do the following:

  1. System > Configuration > Developer (Advanced) > Debug > Profiler set to Yes
  2. index.php file in your magento root: uncomment Varien_Profiler::enable()

Sometimes the profiler output is still not seen on the frontend. In that case I would recommend do a little hack: open up app/code/core/Mage/Core/Block/Profiler.php and comment out the following block:

magento profiler

Sometimes the profiler output is still not seen on the frontend. In that case I would recommend do a little hack: open up app/code/core/Mage/Core/Block/Profiler.php and comment out the following block:

so that it looks like:

Clear your magento cache – you should see the magento profiler output at the bottom of your website pages afterwards.

magento prifiler output

Magento Profiler Output

After enabling profiler at the bottom of every magento webpage you should see the tabular output. I will describe the columns it contains:

  1. Code Profiler – The string that was used in Varien_Profiler::start()  and Varien_Profiler::stop()  function calls.
  2. Time -  The overall time in seconds it took to run code block inside Varien_Profiler::start('Code Profiler')  and Varien_Profiler::stop('Code Profiler')  calls.
  3. Cnt – Number of times the code block inside Varien_Profiler::start('Code Profiler') and Varien_Profiler::stop('Code Profiler')  was run.
  4. Emalloc – Amount of memory in kB that was allocated with emalloc system call.
  5. RealMem – Real memory consumption in kB by php process.

Custom Magento Profiler Usage

Great thing about magento is that it could be extended easily. You could profile your own theme or module code simply by placing Varien_Profiler::start('your_code_profiler') and Varien_Profiler::stop('your_code_profiler')  around it:

Custom Magento Profiler Usage

Great thing about magento is that it could be extended easily. You could profile your own theme or module code simply by placing Varien_Profiler::start('your_code_profiler')  and Varien_Profiler::stop('your_code_profiler')  around it:

Reduce TTFB using Magento Profiler

You will be able to reduce time to first byte with the help of Magento Profiler. Analyze profiler output at the bottom of your website page and see which code profiler string takes most time. Find the place in the code base where that code profiler string is used and analyze the php block inside it. You can use find command in terminal:

Disable unnecessarily magento extensions

Magento has lots of core extensions that are enabled by default. Some of those modules have observers that are run on every page request slowing down your website. By examinig the observers in the profiler output you could decide which ones could be disabled to speed up magento.

Check the profiler output carefully – you might as well find custom modules that were installed long ago but are not used at the moment. They might have observers you were not aware of that are run on every page load. Many times I lower my clients website TTFB by simply disabling unneeded custom extensions.

Here is the list of core extensions you might not need:

  • Mage_Log (if you don’t need magento logging)
  • Mage_Downloadable (if you don’t have downloadable products in your catalog)
  • Mage_Paypal (if you don’t use paypal as the payment option)
  • Mage_Bundle (if you don’t have bundled products in your catalog)
  • Mage_Wishlist (if you don’t need magento wishlist functionality)
  • Mage_SalesRule (if you don’t need coupons or discounts)
  • Mage_Reviews (if you don’t need review functionality on your website)

Review .phtml theme files

Poor code leads to poor performance. If in magento profiler output you see .phtml file that takes more than 0.5s to run – it is time to review that file carefully. Always pay attention to loops – if written badly it might slow your website drastically. I will give you an example I see often while optimizing magento websites:

You do not need to load catalog/product model on every loop cycle – $_collection  is already collection of catalog/product models.

Once I saw product/view.phtml file that had many loops generating html but that html was placed inside display:none  block to be never seen.

Also pay attention to Cnt column – if some code block is run many times maybe there is a room for optimization.

It is important to keep your .phtml theme files code healthy and optimized in order to get low TTFB.

Pick a good Magento hosting

Of course hosting is very important, running your site on a fast hosting will improve load speed significantly. In this Magento hosting review article, you will find a hosting that best fits your need. Remember to get a hosting that offer best load speed to improve Magento load speed

Upgrade magento to the latest version

Keeping magento up-to-date might as well lower your TTFB. Every release contains code improvements that are meant to optimize the way magento works. Perform magento upgrade to make sure you benefit from all the patches there were introduced by core development team.

Summary

Optimization of magento website should start with analyzing Magento Profiler output on every page and bringing TTFB as low as possible. Disable unnecessarily extensions, audit .phtml theme files, make sure you run the latest magento release. Reducing time to first byte might be a complex task but as a result you get fast e-commerce website, more satisfied customers and high search engine rank.

About Konstantin

Konstantin
I am Konstantin and I speed up magento websites on regular basis. If you are struggling with slow website contact me today – I can help!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top