5 Magento Coding Recommendation for optimized performance

Regardless of the e-commerce system you have been working upon, system performance is the final barometer of success. It will make all the difference between your efforts and customer satisfaction. It is important to note that modern customers have several options. There won’t stick to a website that disappoints them in any way. As a result, any error in codes can critically impair businesses. In this article we look at some of the most common coding issues on Magento PHP framework. Understanding the issues will lead to a better website optimization and performance.
 
Before we go further, readers will also want to know that all these issues have been mined and selected from more than 300 coding audits. 28% of the issues have an impact on the scalability and performance of the ecommerce site. Also, these issues collectively represent 84% performance issues experienced with client databases. In general, these relate to memory misuse, useless and redundant computations and inefficient operations.
 
1. Array size measurement with every loop iteration
 
The chances of coding errors are magnified in case a code is located inside loops. This is further facilitated by redundant calculation of SQL queries within the loops, resulting in a huge performance bottleneck.
 
A common example will be calling count () under a condition loop:
 
for ($i = 0; $i < count($rows); $i++ {           //some code   }   Count () is a general function but when used inside loops, the function is slowed down drastically.since PHP doesn’t automatically call for loop-invariant coding, it would be much better to move the function outside the loop:   $rowNum = count($rows);   for ($i = 0; $i < $rowNum; $i++ {           //some code   }   2. SQL Queries inside Loops
 
SQL queries are the most expensive operations and this also has the maximum risk of resulting in a performance bottleneck. In majority of cases, Magento developers load models inside loops as in they use the whole list of product IDs for loading product models and processing it in loops.
 
foreach ($this->getProductIds() as $productId) {
 
        $product = Mage::getModel(‘catalog/product’)->load($productId);
 
        $this->processProduct($product);
 
}
 
Entity Attribute Value models require heavy queries making the process slow. Instead of using loops, Magento Data Collection can be used to make the process more efficient and fast. This will look like:
 
$collection = Mage::getResourceModel(‘catalog/product_collection’)
 
        ->addFieldToFilter(‘entity_id’, array($this->getProductIds()))
 
        ->addAttributeToSelect(array(‘name’));
 
foreach ($collection as $product) {
 
        $this->processProduct($product);
 
}
 
The difference in speed is quite significant. Since saving and deletion of models are also expensive, it is better to have a mass save/delete condition, rather than opting for a loop.
 
3. Loading One Model Several Times
 
Develops must keep in mind that model loading operations aren’t internally cached. Every time, the program calls a load () method, it results in performance degradation as queries run against the database.
 
$name = Mage::getModel(‘catalog/product’)->load($productId)->getName();
 
$sku = Mage::getModel(‘catalog/product’)->load($productId)->getSku();
 
$attr = Mage::getModel(‘catalog/product’)->load($productId)->getAttr();
 
Alternatively, it can be optimized as:
 
$product = Mage::getModel(‘catalog/product’)->load($productId);
 
$name = $product->getName();
 
$sku = $product->getSku();
 
$attr = $product->getAttr();
 
4. Redundant Utilization of Data Set
 
There are huge sets of operations offered under Magento collections. While they are itself quite efficient and optimized, processing large sets of data at ones will call in a load of network and system resources. It is very important to restrict results to the proper limits using filters. Misusing Magento collection is a common problem.
 
A wrong approach would be:
 
public function getRandomItem() {
 
        $collection = Mage::getResourceModel(‘mymodule/my_collection’)->setRandomOrder();
 
        return $collection->getFirstItem();
 
}
 
Alternatively, applying a limitation will require these changes:
 
public function getRandomItem() {
 
        $collection = Mage::getResourceModel(‘mymodule/my_collection’)->setRandomOrder();
 
        ->setPageSize(1);
 
        return $collection->getFirstItem();
 
}
 
5. Inefficient Memory Utilization
 
Lastly, using fetchAll() (database adapter method) to address large collections will cause heavy processing and slow down network resources and the system. The common usage is:
 
$rowSet = $this->_getReadAdapter()->fetchAll($select);
 
foreach ($rowSet as $row) {
 
        //process row
 
}
 
However, separately using the fetch () operator will reduce the consumption of resources. The resulting code will look like:
 
$query = $this->_getReadAdapter()->query)$select);
 
While ($row = $query->fetch()) {
 
        //process row
 
}
 
The records will be retrieved one at a time and the processing will be a lot faster
 
It is always good to optimize codes for better and faster performance. Consumers and end users detest a slow site and the business suffers as a result. Think ahead and scale the future loads.
 

Ready to Take the Next Step?


icons

Promatics India

Content Writer

Promatics since its inception has been committed to deliver services that surpass excellence and tailored to cater the needs of an ever-evolving digital landscape. Promatics designs, develops and delivers web and mobile applications that drive today’s businesses, ameliorate and enhance business capability, reduce customer acquisition lead times, accelerate top line growth, create better brand and ultimately beat competition. Supported by excogitative research and development, Promatics uses its strengths in technology, software, mobile as well as customer service to create new revenue-generating opportunities for its customers and at the same time reducing the overheads, while enabling them to quickly deploy and better manage and direct their businesses.

Still have your concerns?

Your concerns are legit, and we know how to deal with them. Hook us up for a discussion, no strings attached, and we will show how we can add value to your operations!

+91-95010-82999 or hi@promaticsindia.com