Loading..
Processing... Please wait...

Product was successfully added to your shopping cart.



Magento Optimization - How to reduce time to first byte (TTFB)

Magento websites might be slow. In many cases the poor performance comes from poor coding. In this article I will show you how to speed up magento by reducing time to first byte (TTFB).

Did you know TTFB affects SEO?

First we need to know what magento is doing behind the scenes. For that reason we will use the built-in magento tool - Magento Profiler. It will show us the list of internal magento calls along with the corresponding run times.

Magento Profiler Ouput Magento Profiler Ouput

 

You can as well place profiler handles in your own code to measure its performance and identify bottlenecks.

How to Enable Magento Profiler

In magento backend go to System > Configuration > Advanced > Developer > Debug > Profiler set to Yes.

Magento Profiler Configuration Magento Profiler Configuration

 

In index.php file uncomment the following line:

Varien_Profiler::enable();
...

Make sure you flush magento cache for magento profiler out to be displayed.

How to Use Profiler in your code

In order to use profiler add Varien_Profiler::start('any_name_here') and Varien_Profiler::stop('any_name_here') to the code block that you want to profile:

Varien_Profiler::start('any_name_here');
....magento code block that is too slow....;
Varien_Profiler::end('any_name_here');
...

Analyze Profiler Output

  • Code Profiler - the handle that is used in Varien_Profiler::start and Varien_Profiler::stop calls
  • Time - time in seconds that takes to run code between Varien_Profiler::start and Varien_Profiler::stop calls with the handle specified in Code Profiler column
  • Cnt - number of times the specified code block was executed during page load
  • Emalloc - memory allocated to PHP process during code block execution by emalloc system call
  • RealMem - real memory allocated to PHP process during code block execution

 

How to reduce TTFB

Disable unnecessary modules

Go to your website homepage and analyze profiler output at the bottom. First look for OBSERVER keywords. Those are the observers that most probably are executed on every page request. Some of them might be disabled - for example if you dont use magento reviews or magento sales rules functionality the corresponding Mage_Review and Mage_SalesRule modules might be disabled.

An approximate list of default magento extensions you might not need:

  • Mage_Reviews
  • Mage_SalesRule
  • Mage_Wishlist
  • Mage_Bundle
  • Mage_Downloadable
  • Mage_Paypal
  • Mage_Log

Magento Profiler - Observer Tag

Also you might have the custom extension installed in the past that you dont use anymore but it still has frontend observer that is executed on every page request slowing your website down.

Optimize code in theme .phtml files

Php code in theme files could be optimized as well. I will give you an example:

<?php $_collection = $this->getLoadedProductCollection(); ?>
<?php foreach($_collection as $_product):?>
 <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()) ?>
<?php echo $_product->getSku() ?>
<?php endforeach; ?>

You don't need to load catalog/product model in foreach loop as $_product variable is already catalog/product model. If collection is big enough you do many many unneeded model loads.

That is just one example of how unoptimized magento php code might be. To reduce time to first byte and speed up magento website you might want to go over every .phtml file and audit the code there. Pay attention to the loop structures.

Upgrade magento to the latest version

Magento team always work on making magento the better e-commerce framework. If you are running old magento version try to perform magento upgrade. Newest releases consists of many core files rewrites that optimize the way magento handles internal logic.

Summary

Reducing time to first byte might be tedious and complex but the result would sure cheer you up. Your website pages would load faster even uncached. Cache != performance so when optimizing magento you should first optimize TTFB and not setup complex cache system that would most probably bring a lot of maintenance headache.

Happy optimizing!

 

 

If you find this post interesting do not hesitate to sign up for our newsletter and join the 811 people who receive Magento news, tips and tricks regularly.

Thank You!

 

 

10 thoughts on “Magento Optimization - How to reduce time to first byte (TTFB)”

  • Kevin

    Enabling flat data products and categories helped me a lot to improve the time to first byte.

  • Michael Hamel

    Thanks for your great tips. I always update Magento to the latest version available. Also, I think that selecting a reliable hosting for your Magento website is the most important step in order to improve the website speed.

  • Konstantin Gerasimov

    what in-depth knowledge you would like me to share?

  • Hemden

    Sounds credible. But I was hoping for more in depth knowledge. Also compiling does not help a lot anymore when using PHP 7 and opcache

  • Peter David Gustafson
    Peter David Gustafson May 18, 2016 at 11:25 pm

    Great post. I heard of you guys being the #1 Magento devs on the planet. Thanks for the insight.

  • Paul

    Trying to speed up our website http://www.audiobuy.hk with magento but the TTFB is always about 2 seconds ??

  • Konstantin Gerasimov

    @Jignesh it should be Varien_Profiler::start and Varien_Profiler::stop not ::end

  • Jignesh Chauhan
    Jignesh Chauhan March 30, 2016 at 3:53 am

    Hello Sir,

    I have put the Varian_Profile start and end into my code. I got this error. Can you please suggest me what is the problem?

    Fatal error: Call to undefined method Varien_Profiler::end() in /home/fashionstore18/public_html/app/design/frontend/smartwave/porto/template/filterproducts/list.phtml on line 227

    Thanks
    Jignesh

  • Esi

    I need someone to work on improving my webpage load speed to 2 secs. Can you do this?

  • Ryan

    Great article! However as junior developer I would like to see more bad examples of code in magento templates and the 'right way' examples.