Skip to content

Commit

Permalink
Fix extract method to avoid recalculating count() for each iteration.
Browse files Browse the repository at this point in the history
Cache the value of `count($attributes)` to avoid recalculating it for each iteration of the loop.

Since this count will never change during the method execution, this change make the code a bit more efficient.
  • Loading branch information
miclf authored and fabpot committed Nov 29, 2013
1 parent a95503a commit 3447f89
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ public function text()
public function extract($attributes)
{
$attributes = (array) $attributes;
$count = count($attributes);

$data = array();
foreach ($this as $node) {
Expand All @@ -516,7 +517,7 @@ public function extract($attributes)
}
}

$data[] = count($attributes) > 1 ? $elements : $elements[0];
$data[] = $count > 1 ? $elements : $elements[0];
}

return $data;
Expand Down

0 comments on commit 3447f89

Please sign in to comment.