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);