Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Created March 6, 2012 23:22
Show Gist options
  • Save sindresorhus/1989724 to your computer and use it in GitHub Desktop.
Save sindresorhus/1989724 to your computer and use it in GitHub Desktop.
TreeWalker performance

Test the performance of TreeWalker compared to other DOM walker methods. Ran each test 1000 times. Try it yourself.

Chrome 17

Method                  Total ms    Average ms
document.TreeWalker     911         0.911
Iterative Traverser     2945        2.945
Recursive Traverser     69521       69.521
XPath query             4996        4.996
querySelectorAll        12860       12.86
getElementsByTagName    1929        1.929

Safari 5

Method                  Total ms    Average ms
document.TreeWalker     597         0.597
Iterative Traverser     1287        1.287
Recursive Traverser     10542       10.542
XPath query             2995        2.995
querySelectorAll        2769        2.769
getElementsByTagName    311         0.311

Firefox 10

Method                  Total ms    Average ms
document.TreeWalker     6404        6.404
Iterative Traverser     3706        3.706
Recursive Traverser     8838        8.838
XPath query             21041       21.041
querySelectorAll        2815        2.815
getElementsByTagName    182         0.182
@xeaone
Copy link

xeaone commented Nov 11, 2016

Nice man, thanks for putting all the work into this.

@mems
Copy link

mems commented Dec 22, 2018

Based on the an archived this version view-source:https://web.archive.org/web/20111216153452/http://jsfiddle.net:80/zaqtg/1/ plus a fix (document.getElementsByTagName("p")document.getElementsByTagName("*")) https://codepen.io/mmems/pen/dwWyxv , I've got:

Chrome 72

Method			Total ms	Average ms
document.TreeWalker	184		0.184
Iterative Traverser	970		0.97
Recursive Traverser	561		0.561
XPath query		1374		1.374
querySelectorAll	691		0.691
getElementsByTagName	109		0.109

Safari 12

Method			Total ms	Average ms
document.TreeWalker	132		0.132
Iterative Traverser	385		0.385
Recursive Traverser	335		0.335
XPath query		913		0.913
querySelectorAll	209		0.209
getElementsByTagName	57		0.057

Firefox 65

Method			Total ms	Average ms
document.TreeWalker	291		0.291
Iterative Traverser	1360		1.36
Recursive Traverser	700		0.7
XPath query		3735		3.735
querySelectorAll	507		0.507
getElementsByTagName	112		0.112

@jayarjo
Copy link

jayarjo commented Dec 29, 2019

That page doesn't exist. :'/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment