1

Approaching loading products from performance point of view. Assuming you want to echo out standard attribute values such as SKU, name, etc. and you are dealing with a large number of products and you are loading them within a loop (assume not a product collection). Anyone know which is the fastest method?

ObjectManager (Yes I know you're not supposed to use this method)

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

ProductRepositoryInterface

protected $productRepository; 
    ...
    public function __construct(
    ...
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        ...
        $this->productRepository= $productRepository;
        ...
    }

    $product = $this->productRepository->getById($id);

ProductFactory

protected $productFactory;  
    ...
    public function __construct(
        ...
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        ...
        $this->productFactory = $productFactory;
        ...
    }
$product = $this->productFactory->create()->load($id);
7
  • I'm assuming Magento\Catalog\Model\ProductRepository::getById is the same speed if not slower as extends from ProductRepositoryInterface. But could be wrong? May 3, 2019 at 16:06
  • 1
    Don't use $this->productFactory->create()->load($id);, this load method is deprecated.
    – Sohel Rana
    May 3, 2019 at 16:07
  • \Magento\Catalog\Model\ProductRepository::getById has caching. Anyone know whether this or \Magento\Catalog\Api\ProductRepositoryInterface::getById is the prefered method? May 3, 2019 at 16:10
  • \Magento\Catalog\Api\ProductRepositoryInterface::getById -> This is prefered method.
    – Sohel Rana
    May 3, 2019 at 16:15
  • 1
    Yes, it is. It has param like $editMode, if you pass this false which is faster. If you need more product then use getList method.
    – Sohel Rana
    May 3, 2019 at 16:42

1 Answer 1

1

In this scenario we can compare only two option ProductRepositoryInterface and Product Factory. So these two options the ProductRepositoryInterface is faster then Product Factory the reason behind that is ProductRepositoryInterface is getting the Data using API

2
  • Do you have any stats to support this? May 3, 2019 at 16:01
  • 2
    \Magento\Catalog\Api\ProductRepositoryInterface is very slow and using lots of memory.
    – Vipin Garg
    Mar 10, 2022 at 5:09

Your Answer

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

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