Skip to content

Commit 3447f89

Browse files
miclffabpot
authored andcommitted
Fix extract method to avoid recalculating count() for each iteration.
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.
1 parent a95503a commit 3447f89

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ public function text()
504504
public function extract($attributes)
505505
{
506506
$attributes = (array) $attributes;
507+
$count = count($attributes);
507508

508509
$data = array();
509510
foreach ($this as $node) {
@@ -516,7 +517,7 @@ public function extract($attributes)
516517
}
517518
}
518519

519-
$data[] = count($attributes) > 1 ? $elements : $elements[0];
520+
$data[] = $count > 1 ? $elements : $elements[0];
520521
}
521522

522523
return $data;

0 commit comments

Comments
 (0)