most efficient way to iterate over all DOM elements?

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.

currently i'm using jQuery and doing this:

$('body *').each(function(){
var $this = $(this);
//do stuff
});

While it works, It seems to cause some lag on the client. It could also be tweaked with a more specific jQuery context like

$('body', '*')

It occurred to me that native javascript is usually faster than jQuery and I found this.

The Vanilla Javascript way you posted is the fastest. It will be faster than the jQuery solution you posted (See my comment on the question). If you're not removing or adding anything to the DOM in your loop and order of traversal doesn't matter, you can also speed it up ever so slightly by iterating in reverse: