XPath vs. querySelectorAll

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

<script src="http://wicked-good-xpath.googlecode.com/files/wgxpath.install.js"></script>
<script>wgxpath.install();</script>
<div class='adiv'>
  test text inside a div!
</div>
<div class='adiv'>
  test text inside a div!
</div>
<div>
  <div>
    <div class='thediv'>
      test test test test test test
      <span>
        this is a span
      </span>
    </div>
  </div>
</div>
<div class='thediv'>
  test text inside a div!
</div>
<div class='adiv'>
  test text inside a div!
</div>
<div>
  <div>
<div class='thediv'>
  test text inside a div!
</div>
</div>
</div>
<h1>HTML Ipsum Presents</h1>
               
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>

<h2>Header Level 2</h2>
               
<ol>
   <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
   <li>Aliquam tincidunt mauris eu risus.</li>
</ol>

<blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote>

<h3>Header Level 3</h3>

<ul>
   <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
   <li>Aliquam tincidunt mauris eu risus.</li>
</ul>

<pre><code>
#header h1 a {
        display: block;
        width: 300px;
        height: 80px;
}
</code></pre>

Preparation code output

test text inside a div!
test text inside a div!
test test test test test test this is a span
test text inside a div!
test text inside a div!
test text inside a div!

HTML Ipsum Presents

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.

Header Level 2

  1. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  2. Aliquam tincidunt mauris eu risus.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.

Header Level 3

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.

#header h1 a { 
	display: block; 
	width: 300px; 
	height: 80px; 
}

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
XPath 1
document.evaluate('./div/div/div[@class="thediv"]/span', document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
pending…
querySelectorAll 1
document.querySelectorAll('body > div > div > div[class="thediv"] > span');
pending…
querySelectorAll 2
document.querySelectorAll('body > div > div > div.thediv > span');
pending…
querySelectorAll 3
document.body.querySelectorAll('div > div > div[class="thediv"] > span');
pending…
XPath 2
document.evaluate('./div/div/div[@class="thediv"]/span', document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
 
pending…
XPath 3
document.evaluate('/body/div/div/div[@class="thediv"]/span', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment