Code Optimisation Techniques in Magento

A vanilla install of Magento will usually have a page load time of under a second, even with all cache types disabled in the admin. As functionality gets added to the Magento application, the more configuration it has to load and there is an decrease in page speed. However, there are many examples of code added which is not fine-tuned and can add up to tenths of a second onto the page load time. There are several code optimisation techniques in Magento that can be carried out to ensure your store loads as quick as it can.

Collections

The first technique is a fairly obvious one involving foreach loops. This loops are commonly used when iterating over collections.

foreach ($this->getProductCollection() as $product) {
    // Some code
}

Whilst the $this->getProductCollection() won’t get called multiple times as it is not in the foreach loop itself, the foreach makes a copy of the array to iterate over, and then iterates over the copy.

Therefore the copying and the subsequent memory usage involved depending on the size of collection is what should be considered.

When handling large collections, often developers will prefer to write the following.

$collection = $this->getProductCollection();
foreach ($collection as $product) {
    // Some code
}

Continuing the collection trend, it is very easy to search for code online that will retrieve a collection, such as the product collection.

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')

But did you know that the asterisk within addAttributeToSelect() will return all product attributes?

If you are only looking to retrieve product names, for example, you can write the following.

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect(array('name'));

Product Loading

In Magento, you can load a product and its data, assuming you have the product ID available, by writing the below.

$product = Mage::getModel('catalog/product')->load(1);

By default, this will load all of the attributes associated to this product. This single line of code is easy to remember and is commonly used within product collection loops to

If however, you only need one or two attributes, you do not need to use the load() method. Instead, use getAttributeRawValue(), which comes from the getResource() method.

$productId = 1;
$_resource = $this->getProduct()->getResource();
$attributeValue = $_resource->getAttributeRawValue($productId, 'custom_attribute_value', Mage::app()->getStore());

Block Caching

Block caching involves Magento storeing the HTML output of the blocks into its cache and subsequently loads the contents directly from it.

Magento includes block caching by default on some of its blocs, such as the footer, but you can also add caching to a custom block type.

This can be done using the setCacheLifetime() method passing in a cache lifetime value in seconds.

You can define this in layout XML.

<block type="vendor_somemodule/someblock" name="somemodule.someblock.cacheexample">
    <action method="setCacheLifetime">
        <lifetime>3600</lifetime>
    </action>
</block>

Or it can also be defined in the _construct() method of a block class.

class Vendor_Somemodule_Block_Someblock extends Mage_Core_Block_Template
{
    protected function _construct()
    {
        $this->setCacheLifetime(3600);
    }
}

Note: This article is based on Magento CE version 1.9.