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);
  • I'm assuming Magento\Catalog\Model\ProductRepository::getById is the same speed if not slower as extends from ProductRepositoryInterface. But could be wrong? – Dominic Xigen May 3 '19 at 16:06
  • 1
    Don't use $this->productFactory->create()->load($id);, this load method is deprecated. – Sohel Rana May 3 '19 at 16:07
  • \Magento\Catalog\Model\ProductRepository::getById has caching. Anyone know whether this or \Magento\Catalog\Api\ProductRepositoryInterface::getById is the prefered method? – Dominic Xigen May 3 '19 at 16:10
  • \Magento\Catalog\Api\ProductRepositoryInterface::getById -> This is prefered method. – Sohel Rana May 3 '19 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 '19 at 16:42
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

| improve this answer | |
  • Do you have any stats to support this? – Dominic Xigen May 3 '19 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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