Last active
December 16, 2015 10:59
-
-
Save airportyh/5424357 to your computer and use it in GitHub Desktop.
Quickly and recursively traverse DOM elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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