title: Most efficient way to iterate over all DOM elements
body: <p>Unfortunately I need to iterate over all the DOM elements of a page and I'm wondering what the most efficient technique is. I could probably benchmark these myself and might if I have the time but I'm hoping someone has already experienced this or has some options I hadn't considered.</p>
  <p>Currently I'm using jQuery and doing this:</p>
  <pre class="lang-js prettyprint-override"><code>$('body *').each(function(){                                                                                                                            
      var $this = $(this);                                                                                                                                
      // do stuff                                                                                                                                         
  });
  </code></pre>
  <p>While it works, It seems to cause some lag on the client. It could also be tweaked with a more specific jQuery context like <code>$('body', '*')</code>. It occurred to me that native Javascript is usually faster than jQuery and I found this:</p>
  <pre class="lang-js prettyprint-override"><code>var items = document.getElementsByTagName(&quot;*&quot;);
  for (var i = 0; i &lt; items.length; i++) {
      // do stuff
  }
  </code></pre>
  <p>I'm assuming the native option is faster. Wondering if there are other options I hadn't considered. Maybe a recursive option that iterates over child nodes in parallel.</p>