Skip to content

Instantly share code, notes, and snippets.

@anthowave
Last active December 24, 2020 10:53
Show Gist options
  • Save anthowave/aa53f5312c3a268f2ffb7bae6b38aaca to your computer and use it in GitHub Desktop.
Save anthowave/aa53f5312c3a268f2ffb7bae6b38aaca to your computer and use it in GitHub Desktop.
6-line javascript code for lazy DFS over the DOM
function* search(node) {
if (!node) return;
yield node;
yield* search(node.firstChild);
yield* search(node.nextSibling);
}
// example usage
for (let node of search(document)) {
if (node.localName === 'title') {
console.log(node.textContent);
break;
}
}
// from: https://www.youtube.com/watch?v=cLxNdLK--yI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment