Skip to content

Instantly share code, notes, and snippets.

@airportyh
Last active December 16, 2015 10:59
Show Gist options
  • Select an option

  • Save airportyh/5424357 to your computer and use it in GitHub Desktop.

Select an option

Save airportyh/5424357 to your computer and use it in GitHub Desktop.
Quickly and recursively traverse DOM elements.
function traverseDom(elm, visit){
visit(elm)
var curr = elm.firstChild
while (curr){
if (curr.nodeType === 1){
traverseDom(curr, visit)
}
curr = curr.nextSibling
}
}
var count = 0
var start = + new Date
traverseDom(document.documentElement, function(elm){
count++
})
var end = + new Date
console.log('Took ' + (end - start) + 'ms')
console.log('Total # of elements = ' + count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment