Last active
January 15, 2024 06:49
-
-
Save ryanmorr/bd025b3b8e1d79091bea2adc2d9b07b6 to your computer and use it in GitHub Desktop.
Fastest way to iterate through each child node of an element and call a function
This file contains 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
// Traversing with firstChild/nextSibling is much faster than childNodes: https://jsperf.app/nextsibling-vs-childnodes | |
function forEachChild(element, callback) { | |
let node = element.firstChild; | |
while (node) { | |
callback(node); | |
node = node.nextSibling; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment