Magento 2 PHP Optimize Tips – Part 1

Magento 2 PHP Optimize Tips – Part 1

Magento 2 PHP Optimize Tips:
There are many ways to optimize Magento 2. However, the first thing you should do is optimizing your codes before touching to DB or Cache.

Tip 1: Using Factory in the constructor.

Why did I say that? Because of the case very simple of Dependence Injection design working in Magento 2

=> _constructor, __constructor … will be called, it’s very bad if in the constructor or default functions call for each Collection.
=> 
If your classes are Factory the loop process will stop at Factory Object.

More info about DI in Magento 2 here: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/depend-inj.html

Tip 2: Using Collection Walker when you are playing with Large Collection. 

  • By default when you get items from Collection, Model will run through all items of the collection, assigned them to Model, then call before/afterload functions. If your collection only gets data from a table or data don’t need to be modified by specific Model, Model items assignment walk will do that for you. The Walk will get raw data for you but no need to call before and afterload.

PHP – Example:

use \Magento\Framework\Model\ResourceModel\Iterator; class ClassName { public function process(){ $extParam = new Object(); $collection = $this->_orderCollectionFactory->create(); $iterator = $objectManager->get(Iterator::class); $iterator->walk($collection->getSelect(), [[$this,'callBack']]); } public function callBack($item, $extParam){ //Do something } }
Code language: PHP (php)

Let test performance with walker and for loop collection.

—-> CollectionWalk

class CollectionWalk { protected $count = 0; public function excute(){ $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** * @@var $collection ZipCodeCollection */ $collection = $objectManager->create(ZipCodeCollection::class); $collection->walk([$this, 'callBack']); return $this->count; } public function callBack($row){ $this->count += 1; } }
Code language: PHP (php)

—->  IteratorWalk

class IteratorWalk { protected $count = 0; public function excute(){ $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $iterator = $objectManager->get(Iterator::class); $collection = $objectManager->create(ZipCodeCollection::class); $iterator->walk($collection->getSelect(), [[$this, 'callBack']]); return $this->count; } public function callBack($args){ $this->count = $args['idx']; } }
Code language: PHP (php)

–> The sample CODE

–> Result.

select count(*) from [Table]; ==> 28617 START PROCESS ITEMS WITH WALK.---------------- 28616 <=== Item Index. Execution time : 0.19032716751099 seconds END PROCESS ITEMS WITH WALK.---------------- START PROCESS ITEMS WITH COLLECTION WALK.--------- 28617 <=== Item Count Execution time : 1.8405199050903 seconds END PROCESS ITEMS WITH COLLECTION WALK.----------- START PROCESS ITEMS WITH FOR LOOP COLLECTION.----- 28617 <=== Item Count Execution time : 1.9223699569702 seconds END PROCESS ITEMS WITH FOR LOOP COLLECTION.-------
Code language: JavaScript (javascript)

Tip 3: Using Customer Data sections instead of Disable Block Cache.

Why?. In case you have a block with data need up to date with every request but your web page was enabled FULL PAGE CACHE or CDN HTML Output Cache. You don’t want to disable full page cache.

Which other Magento version as 2.0.x, 2.1.x we can use an option for this case make your block reload with ajax that is: $_isScopePrivate but with higher Magento versions this option doesn’t work properly.

Do not use the $_isScopePrivate property in your blocks. This property is obsolete and won’t work properly. (Magento Comunity)

You need to play with Customer Data Sections if you want to your block update data with FULL PAGE CACHE enabled.  Read more here>>> https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching/private-content.html

Thanks for reading.

Any feedback please leave a comment below this post.

— To be continued —

7 thoughts on “Magento 2 PHP Optimize Tips – Part 1

Leave a Reply

Your email address will not be published. Required fields are marked *