Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
NodeIterator vs TreeWalker for Component Extraction (FIXED)
(version: 0)
previous version used TreeWalker for both /facepalm
Comparing performance of:
NodeIterator vs TreeWalker
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// language=HTML const html = ` <body> <h1>Doc with mix of light and shadow DOM custom elements</h1> <p>NOTE: we rely on Chrome's support for "declarative shadow DOM"</p> <x-light id="1"> <!-- comment --> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum deserunt dolor nesciunt ab placeat nostrum. Laborum rem quisquam ullam, officiis maiores fugiat fuga animi quos. Ex earum a alias magni. </x-light> <x-shadow id="2"> <template shadowroot="open"> <!-- comment --> <p> Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ullam amet error delectus... </p> <slot></slot> </template> <!-- comment --> Impedit nesciunt excepturi perferendis quis in debitis vitae soluta voluptatibus praesentium repellat labore quas. Cum unde consectetur maiores. </x-shadow> <x-light id="3"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum deserunt dolor nesciunt ab placeat nostrum. Laborum rem quisquam ullam, officiis maiores fugiat fuga animi quos. Ex earum a alias magni. <x-light id="3.1"> <span>Lorem ipsum dolor sit amet</span> consectetur adipisicing elit. Rerum deserunt dolor nesciunt ab placeat nostrum. Laborum rem quisquam ullam, officiis maiores fugiat fuga animi quos. Ex earum a alias magni. <x-light id="3.1.1"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum deserunt dolor nesciunt ab placeat nostrum. Laborum rem quisquam ullam, officiis maiores fugiat fuga animi quos. Ex earum a alias magni. </x-light> </x-light> </x-light> <x-shadow id="4"> <template shadowroot="open"> <x-shadow id="4.1"> <template shadowroot="open"> Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ullam amet error delectus... <slot></slot> <x-shadow id="4.1.1"> <template shadowroot="open"> Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ullam amet error delectus... <slot></slot> </template> Impedit nesciunt excepturi perferendis quis in debitis vitae soluta voluptatibus praesentium repellat labore quas. Cum unde consectetur maiores. <x-light id="4.1.1.1"> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum deserunt dolor nesciunt ab placeat nostrum. Laborum rem quisquam ullam, officiis maiores fugiat fuga animi quos. Ex earum a alias magni. </p> </x-light> </x-shadow> </template> Impedit nesciunt excepturi perferendis quis in debitis vitae soluta voluptatibus praesentium repellat labore quas. Cum unde consectetur maiores. </x-shadow> <slot></slot> </template> <x-shadow id="5"> <template shadowroot="open"> Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ullam amet error delectus... <slot></slot> </template> Impedit nesciunt excepturi perferendis quis in debitis vitae soluta voluptatibus praesentium repellat labore quas. Cum unde consectetur maiores. </x-shadow> </x-shadow> </body> `; const fragment = new DOMParser().parseFromString(html, 'text/html', { includeShadowRoots: true }); document.body.replaceChildren(fragment.body); function getAllComponents(walkerFunc, root = document.body, shadowParent) { const components = [] const lightParentStack = [] let element, customElementType, customElementName, customElement const walker = walkerFunc(root) while ((element = walker.nextNode())) { if ( (hasShadowRoot = !!element.shadowRoot) || (customElementType = getCustomElementType(element)) ) { while ( lightParentStack.length && !isAncestor(element, lightParentStack[0].element) ) { lightParentStack.shift() } lightParent = lightParentStack[0] if (customElementType) { customElementName = customElementType === 'built-in' ? element.getAttribute('is') : element.tagName.toLowerCase() customElement = { type: customElementType, name: customElementName, definition: customElements.get(customElementName), } } else { customElement = false } const component = { element, hasShadowRoot, customElement, shadowParent, lightParent, shadowChildren: [], lightChildren: [], } if (shadowParent) { shadowParent.shadowChildren.push(component) } if (lightParent) { lightParent.lightChildren.push(component) } components.push(component) // regardless of hasShadowRoot, an element can have light DOM children lightParentStack.unshift(component) if (component.hasShadowRoot) { components.push( ...getAllComponents(walkerFunc, element.shadowRoot, component) ) } } } return components } function getCustomElementType(el) { return el.tagName.includes('-') ? 'autonomous' : el.hasAttribute('is') ? 'built-in' : null } function isAncestor(child, ancestor) { let parent = child.parentNode while (parent) { if (parent === ancestor) { return true } parent = parent.parentNode } return false } function createComponentIterator(root) { return document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT, null) } function createComponentTreeWalker(root) { return document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, null) }
Tests:
NodeIterator
console.log(getAllComponents(createComponentIterator))
TreeWalker
console.log(getAllComponents(createComponentTreeWalker))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
NodeIterator
TreeWalker
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Based on the provided information, I'll attempt to answer your question. It appears that you're asking about the performance comparison between two different methods of traversing an HTML document: `NodeIterator` and `TreeWalker`. Given the benchmark results, it seems that the `NodeIterator` approach is faster than the `TreeWalker` approach. The `ExecutionsPerSecond` values for `NodeIterator` are significantly higher (2933.44775390625) compared to `TreeWalker` (2044.955078125). There are several possible reasons for this performance difference: 1. **Implementation differences**: NodeIterator and TreeWalker might be implemented differently in the Chrome browser, which could affect their performance. 2. **Memory usage**: NodeIterator might require less memory than TreeWalker, especially when dealing with large HTML documents. 3. **DOM traversal strategy**: The two methods might traverse the DOM differently, which could impact performance. To further investigate this issue, I would recommend examining the Chrome browser's implementation of `NodeIterator` and `TreeWalker`, as well as optimizing the code that uses these methods to minimize any potential overhead. If you'd like me to elaborate on any of these points or provide more suggestions for improving performance, feel free to ask!
Related benchmarks:
Traverse function vs NodeIterator vs TreeWalker vs TreeWalker manual filter
TreeWalker vs querySelectorAll (* all elements)2
Traverse function vs NodeIterator vs TreeWalker for Custom Element Search
NodeIterator vs TreeWalker for Component Extraction
Comments
Confirm delete:
Do you really want to delete benchmark?