1

Given two ways to loading a category collection:

$children = Mage::getModel('catalog/category')->getCollection();
$children->addAttributeToSelect('name')
    ->addAttributeToFilter('parent_id', $parent_id)
    ->addAttributeToFilter('is_active', 1)
    ->addAttributeToFilter('include_in_menu', 1)
    ->addAttributeToSort('position');

Or

$children = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('parent_id', $parent_id)
    ->addAttributeToFilter('is_active', 1)
    ->addAttributeToFilter('include_in_menu', 1)
    ->addAttributeToSort('position');

Are there any differences in performance between them?

2
  • I'd've thought that any execution timing improvement would be minute by using the second example. Nov 13, 2014 at 11:47
  • i guess second one take less time
    – Amit Bera
    Nov 13, 2014 at 11:47

2 Answers 2

1

There is no significant difference between the 2 of them.
Through the chaining mechanism, each method is applied to the result of the previous method.
So both codes do the same thing and they are both similar to:

$children = Mage::getModel('catalog/category')->getCollection();
$children->addAttributeToSelect('name');
$children->addAttributeToFilter('parent_id', $parent_id);
$children->addAttributeToFilter('is_active', 1);
$children->addAttributeToFilter('include_in_menu', 1);
$children->addAttributeToSort('position');

This happens because the methods addAttributeToSelect addAttributeToFilter and addAttributeToSort return $this...the instance of the current object.
Internally "stuff" may happen differently in all the cases, but I have no idea how the PHP interpreter works.

The result is the same and the time it takes should not have a significant difference.

1

As far as I know there shouldn't be any difference what so ever but if you want to benchmark you can use Magento's build in Varien Profiler, read more about that here.

Varien_Profiler::start(‘collection-method-one’); //start the profiler 

$children = Mage::getModel('catalog/category')->getCollection();
$children->addAttributeToSelect('name')
    ->addAttributeToFilter('parent_id', $parent_id)
    ->addAttributeToFilter('is_active', 1)
    ->addAttributeToFilter('include_in_menu', 1)
    ->addAttributeToSort('position');

Varien_Profiler::stop(‘your-key’);// end the profiler

Varien_Profiler::start(‘collection-method-two’); //start the profiler 

$children = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('parent_id', $parent_id)
    ->addAttributeToFilter('is_active', 1)
    ->addAttributeToFilter('include_in_menu', 1)
    ->addAttributeToSort('position');

Varien_Profiler::stop(‘your-two’);// end the profiler

Now turn on the profiler in the backend under System > Configuration > Developer and see what comes out...

1
  • Yeah...profiler. totally forgot about that.
    – Marius
    Nov 13, 2014 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.