Skip to content

Avoid calling function repetitively in loop conditionals for better performance #16142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/code/Magento/Backend/Block/Dashboard/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function getChartUrl($directUrl = true)
$thisdataarray = $serie;
if ($this->_encoding == "s") {
// SIMPLE ENCODING
for ($j = 0; $j < sizeof($thisdataarray); $j++) {
for ($j = 0, $jMax = count($thisdataarray); $j < $jMax; $j++) {
$currentvalue = $thisdataarray[$j];
if (is_numeric($currentvalue)) {
$ylocation = round(
Expand All @@ -344,7 +344,7 @@ public function getChartUrl($directUrl = true)
}
} else {
// EXTENDED ENCODING
for ($j = 0; $j < sizeof($thisdataarray); $j++) {
for ($j = 0, $jMax = count($thisdataarray); $j < $jMax; $j++) {
$currentvalue = $thisdataarray[$j];
if (is_numeric($currentvalue)) {
if ($yrange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function testTierPrices($priceScope, $expectedWebsiteId)
$this->assertTrue(is_array($tpArray));
$this->assertEquals(sizeof($tps), sizeof($tpArray));

for ($i = 0; $i < sizeof($tps); $i++) {
for ($i = 0, $iMax = count($tps); $i < $iMax; $i++) {
$tpData = $tpArray[$i];
$this->assertEquals($expectedWebsiteId, $tpData['website_id'], 'Website Id does not match');
$this->assertEquals($tps[$i]->getValue(), $tpData['price'], 'Price/Value does not match');
Expand Down Expand Up @@ -233,7 +233,7 @@ public function testTierPrices($priceScope, $expectedWebsiteId)
$this->assertEquals(50, $tpRest->getExtensionAttributes()->getPercentageValue());
}

for ($i = 0; $i < sizeof($tps); $i++) {
for ($i = 0, $iMax = count($tps); $i < $iMax; $i++) {
$this->assertEquals(
$tps[$i]->getValue(),
$tpRests[$i]->getValue(),
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Payment/Model/Method/Cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function validateCcNum($ccNumber)
$cardNumber = strrev($ccNumber);
$numSum = 0;

for ($i = 0; $i < strlen($cardNumber); $i++) {
for ($i = 0, $iMax = strlen($cardNumber); $i < $iMax; $i++) {
$currentNum = substr($cardNumber, $i, 1);

/**
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Sales/Model/Order/Pdf/AbstractPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function widthForStringUsingFontSize($string, $font, $fontSize)
);

$characters = [];
for ($i = 0; $i < strlen($drawingString); $i++) {
for ($i = 0, $iMax = strlen($drawingString); $i < $iMax; $i++) {
$characters[] = ord($drawingString[$i++]) << 8 | ord($drawingString[$i]);
}
$glyphs = $font->glyphNumbersForCharacters($characters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testDictionaryGetter()
}

$file = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\File\ReadInterface::class);
for ($i = 0; $i < count($data); $i++) {
for ($i = 0, $iMax = count($data); $i < $iMax; $i++) {
$file->expects($this->at($i))->method('readCsv')->will($this->returnValue($data[$i]));
}
$file->expects($this->at($i))->method('readCsv')->will($this->returnValue(false));
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function pack($source, $destination = 'packed.tgz', $skipRoot = false)
{
$archivers = $this->_getArchivers($destination);
$interimSource = '';
for ($i = 0; $i < count($archivers); $i++) {
for ($i = 0, $iMax = count($archivers); $i < $iMax; $i++) {
if ($i == count($archivers) - 1) {
$packed = $destination;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getCleanPath($path)
$pathParts = explode("/", $pathTokP);
$realPathParts = [];

for ($i = 0, $realPathParts = []; $i < count($pathParts); $i++) {
for ($i = 0, $realPathParts = [], $iMax = count($pathParts); $i < $iMax; $i++) {
if ($pathParts[$i] == '.') {
continue;
} elseif ($pathParts[$i] == '..') {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Filter/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected function getVariable($value, $default = '{no_value_defined}')
$stackVars = $tokenizer->tokenize();
$result = $default;
$last = 0;
for ($i = 0; $i < count($stackVars); $i++) {
for ($i = 0, $iMax = count($stackVars); $i < $iMax; $i++) {
if ($i == 0 && isset($this->templateVars[$stackVars[$i]['name']])) {
// Getting of template value
$stackVars[$i]['variable'] = & $this->templateVars[$stackVars[$i]['name']];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ protected function _getOrderedTimerIds()

$prevTimerId = $timerIds[0];
$result = [$prevTimerId];
for ($i = 1; $i < count($timerIds); $i++) {
for ($i = 1, $iMax = count($timerIds); $i < $iMax; $i++) {
$timerId = $timerIds[$i];
/* Skip already added timer */
if (!$timerId) {
Expand All @@ -209,7 +209,7 @@ protected function _getOrderedTimerIds()
/* Loop over all timers that need to be closed under previous timer */
while (strpos($timerId, $prevTimerId . Profiler::NESTING_SEPARATOR) !== 0) {
/* Add to result all timers nested in the previous timer */
for ($j = $i + 1; $j < count($timerIds); $j++) {
for ($j = $i + 1, $jMax = count($timerIds); $j < $jMax; $j++) {
if (strpos($timerIds[$j], $prevTimerId . Profiler::NESTING_SEPARATOR) === 0) {
$result[] = $timerIds[$j];
/* Mark timer as already added */
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/System/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function mkdirRecursive($path, $mode = 0777)
$dir = explode("/", $path);
$path = "";
$ret = true;
for ($i = 0; $i < count($dir); $i++) {
for ($i = 0, $iMax = count($dir); $i < $iMax; $i++) {
$path .= "/" . $dir[$i];
if (!@ftp_chdir($this->_conn, $path)) {
@ftp_chdir($this->_conn, "/");
Expand Down
4 changes: 2 additions & 2 deletions setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function _parse()

$results = [];
preg_match_all(Filter::CONSTRUCTION_PATTERN, $data, $results, PREG_SET_ORDER);
for ($i = 0; $i < count($results); $i++) {
for ($i = 0, $iMax = count($results); $i < $iMax; $i++) {
if ($results[$i][1] === Filter::TRANS_DIRECTIVE_NAME) {
$directive = [];
if (preg_match(Filter::TRANS_DIRECTIVE_REGEX, $results[$i][2], $directive) !== 1) {
Expand All @@ -43,7 +43,7 @@ protected function _parse()
}

preg_match_all(self::HTML_FILTER, $data, $results, PREG_SET_ORDER);
for ($i = 0; $i < count($results); $i++) {
for ($i = 0, $iMax = count($results); $i < $iMax; $i++) {
if (!empty($results[$i]['value'])) {
$this->_addPhrase($results[$i]['value']);
}
Expand Down
4 changes: 2 additions & 2 deletions setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Js.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ protected function _parse()
$fileRow = fgets($fileHandle, 4096);
$results = [];
preg_match_all('/mage\.__\(\s*([\'"])(.*?[^\\\])\1.*?[),]/', $fileRow, $results, PREG_SET_ORDER);
for ($i = 0; $i < count($results); $i++) {
for ($i = 0, $iMax = count($results); $i < $iMax; $i++) {
if (isset($results[$i][2])) {
$quote = $results[$i][1];
$this->_addPhrase($quote . $results[$i][2] . $quote, $lineNumber);
}
}

preg_match_all('/\\$t\(\s*([\'"])(.*?[^\\\])\1.*?[),]/', $fileRow, $results, PREG_SET_ORDER);
for ($i = 0; $i < count($results); $i++) {
for ($i = 0, $iMax = count($results); $i < $iMax; $i++) {
if (isset($results[$i][2])) {
$quote = $results[$i][1];
$this->_addPhrase($quote . $results[$i][2] . $quote, $lineNumber);
Expand Down