Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TreeWalker
(version: 0)
Comparing performance of:
Better Solution vs Optimal Solution vs TreeWalker vs TreeWalker with LCA vs Creative Solution
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div> <span id="start"> Hello </span> <div> <img src="http://placehold.it/100x75" /> </div> <div> <span> include me </span> </div> <span id="end">World</span> <span>don't include me</span> </div>
Tests:
Better Solution
function getPath(a) { var path = []; while (a !== null) { path.push(a); a = a.parentNode; } path.reverse(); return path; } function getLCA(a,b) { var aPath = getPath(a); var bPath = getPath(b); var lca; for (var i = 0; i < aPath.length && aPath[i] === bPath[i]; i++) { lca = aPath[i]; } return lca; } function getHighlightedText(a, b) { var passedA = false; var passedB = false; var text = ''; var lca = getLCA(a,b); function traverse(node) { if (node === a) { passedA = true; } if (passedA && node.nodeType === 3) { text += node.nodeValue; } if (node === b) { passedB = true; return; } var child = node.firstChild; while (child !== null && !passedB) { traverse(child); child = child.nextSibling; } } traverse(lca); return text; } getHighlightedText( document.getElementById('start').firstChild, document.getElementById('end').firstChild );
Optimal Solution
function getHighlightedText(a, b) { var text = a.nodeValue; var cur = a; while (cur !== b) { if (cur.firstChild !== null) { cur = cur.firstChild; } else { while (cur.nextSibling === null) { cur = cur.parentNode; } cur = cur.nextSibling; } if (cur.nodeType === 3) { text += cur.nodeValue; } } return text; } getHighlightedText( document.getElementById('start').firstChild, document.getElementById('end').firstChild );
TreeWalker
function getHighlightedText(a, b) { var passedA = false; var text = ''; const treeWalker = document.createTreeWalker( document.body, NodeFilter.SHOW_TEXT ); while (treeWalker.nextNode()) { const node = treeWalker.currentNode; if (node === a) { passedA = true; } if (passedA) { text += node.nodeValue; } if (node === b) { break; } } return text; } getHighlightedText( document.getElementById('start').firstChild, document.getElementById('end').firstChild );
TreeWalker with LCA
function getPath(a) { var path = []; while (a !== null) { path.push(a); a = a.parentNode; } path.reverse(); return path; } function getLCA(a,b) { var aPath = getPath(a); var bPath = getPath(b); var lca; for (var i = 0; i < aPath.length && aPath[i] === bPath[i]; i++) { lca = aPath[i]; } return lca; } function getHighlightedText(a, b) { var passedA = false; var text = ''; var lca = getLCA(a,b); const treeWalker = document.createTreeWalker( lca, NodeFilter.SHOW_TEXT ); while (treeWalker.nextNode()) { const node = treeWalker.currentNode; if (node === a) { passedA = true; } if (passedA) { text += node.nodeValue; } if (node === b) { break; } } return text; } getHighlightedText( document.getElementById('start').firstChild, document.getElementById('end').firstChild );
Creative Solution
function getHighlightedText(a, b) { var all = document.getElementsByTagName('*'); var filtered = ""; var aPassed = false; for (var i = 0; i < all.length; i++) { var elem = all[i]; if (elem.firstChild === a) { aPassed = true; } if (elem.firstChild && elem.firstChild.nodeType === 3 && aPassed) { filtered += elem.firstChild.nodeValue; } if (elem.firstChild == b) { break; } }; return filtered; } getHighlightedText( document.getElementById('start').firstChild, document.getElementById('end').firstChild );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Better Solution
Optimal Solution
TreeWalker
TreeWalker with LCA
Creative Solution
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):
The final answer is: Based on the benchmark results, the most efficient solution for extracting highlighted text between two elements is the "Optimal Solution", with an average of 586655 executions per second. This solution uses a more efficient algorithm to traverse the DOM and extract the required text. Here's a brief summary of the solutions' performance: * Optimal Solution: 586655 executions per second * TreeWalker: 414573 executions per second * Better Solution: 358983 executions per second * TreeWalker with LCA: 312832 executions per second * Creative Solution: 204264 executions per second The "Optimal Solution" clearly outperforms the other solutions, indicating that it is the most efficient and effective approach for this specific task.
Related benchmarks:
treeWalker param vs treefalker with filter function vs querySelectorAll
treeWalker param vs treefalker with filter function vs querySelectorAll edit
Copy of TreeWalker vs querySelectorAll (* all elements)
Fork TreeWalker vs querySelectorAll (* all elements)2
Comments
Confirm delete:
Do you really want to delete benchmark?