what is the most fast, safe and correct method for looping through whole DOM in pure JS?
By safe I mean the fact that NodeList created by `document.getElementsById('*');` is a live object. Some elements are added or removed dynamically as JS engine loops through output of this function.

The thing I intend to do is loop over a whole DOM (this would be in a greasemonkey script, so not for normal web use, but I think it also applies there), check if a node CSS match some rules and according to that change the CSS.

This process takes some time and since the website has multiple scripts running on it, constantly adding and removing DOM nodes sooner or later there will be a case when a node will disappear right when it is processed.

And by fast I mean is it better to do it recursively:

    function traverseDOM(node,f) {
      if (node.nodeType !== 1) return;
      f.apply(node);
      for (var i = 0; i < node.childNodes.length; i++) {
        traverseDOM(node.childNodes[i],f);
      }
    }

(but this will waste time on checking nodeType)
or by `for` loop:

    nodes = document.getElementsByTagName('*');
    for (var i=0; i<nodes.length; i++) {
      //do my stuff
    }
(but this would have issues at the end where it will encounter fluctuating tip of NodeList)
what is the most fast, safe and correct method for looping through whole DOM in pure JS?
===

By safe I mean the fact that NodeList created by `document.getElementsById('*');` is a live object. Some elements are added or removed dynamically as JS engine loops through output of this function.

The thing I intend to do is loop over a whole DOM (this would be in a greasemonkey script, so not for normal web use, but I think it also applies there), check if a node CSS match some rules and according to that change the CSS.

This process takes some time and since the website has multiple scripts running on it, constantly adding and removing DOM nodes sooner or later there will be a case when a node will disappear right when it is processed.

And by fast I mean is it better to do it recursively:

    function traverseDOM(node,f) {
      if (node.nodeType !== 1) return;
      f.apply(node);
      for (var i = 0; i < node.childNodes.length; i++) {
        traverseDOM(node.childNodes[i],f);
      }
    }

(but this will waste time on checking nodeType)
or by `for` loop:

    nodes = document.getElementsByTagName('*');
    for (var i=0; i<nodes.length; i++) {
      //do my stuff
    }
(but this would have issues at the end where it will encounter fluctuating tip of NodeList)