`XPath::query()` is much more efficient than `getElementByTagName()`
authorAlexander Ebert <ebert@woltlab.com>
Mon, 12 Apr 2021 19:42:29 +0000 (21:42 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 12 Apr 2021 19:42:29 +0000 (21:42 +0200)
The test document with ~8k elements took ~0.6s using `getElementsByTagName(*)`, but only 0.03s using `query("//*")`

wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php

index f3fdfe0f3c424fc92e66e97d3ffeb11e972a753f..6e4cf7222996dc5ad426b88eb195c1daac750032 100644 (file)
@@ -304,7 +304,7 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor {
                
                $tags = [];
                /** @var \DOMElement $tag */
-               foreach ($this->getDocument()->getElementsByTagName('*') as $tag) {
+               foreach ($this->getXPath()->query("//*") as $tag) {
                        $tagName = $tag->nodeName;
                        if (!isset($tags[$tagName])) $tags[$tagName] = $tagName;
                }