Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Last active January 15, 2024 06:49
Show Gist options
  • Save ryanmorr/bd025b3b8e1d79091bea2adc2d9b07b6 to your computer and use it in GitHub Desktop.
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
// 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