Class find WholePageNative vs LimitedScope

JavaScript performance comparison

Test case created by CS

Info

When searching for a unique class on the page, is it better to just search the entire document (so the JQ selector will clearly map down to the native browser function getElementsByClassName), or is it better to limit the scope of the search to a smaller area but risk JQ having to do an inefficient manual search.

Preparation code

<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a"><a href="http://www.notme.com">A</a></li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1"><a href="http://www.notme.com">1</a></li>
          <li class="item-2"><a href="http://www.notme.com">2</a></li>
          <li class="item-3"><a href="http://www.notme.com">3</a></li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii"><a href="http://www.notme.com">III</a></li>
</ul>
<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a"><a href="http://www.notme.com">A</a></li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1"><a href="http://www.notme.com">1</a></li>
          <li class="item-2"><a href="http://www.notme.com">2</a></li>
          <li class="item-3"><a href="http://www.notme.com">3</a></li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii"><a href="http://www.notme.com">III</a></li>
</ul>
<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a"><a href="http://www.notme.com">A</a></li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1"><a href="http://www.notme.com">1</a></li>
          <li class="item-2"><a href="http://www.notme.com">2</a></li>
          <li class="item-3"><a href="http://www.notme.com">3</a></li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii"><a href="http://www.notme.com">III</a></li>
</ul>
<ul class="level-1" id="OuterStart">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a">A
        <ul class="level-3a" id="StartHereA">
          <li class="item-0a"><a href="http://www.notme.com">0</a></li>
          <li class="item-1a"><a href="http://www.notme.com">1</a></li>
          <li class="item-2a"><a href="http://www.findme.com" class="foo">2</a></li>
          <li class="item-3a"><a href="http://www.notme.com">3</a></li>
          <li class="item-4a"><a href="http://www.notme.com">4</a></li>
        </ul>
      </li>
      <li class="item-b">B
        <ul class="level-3b" id="StartHereB">
          <li class="item-0b"><a href="http://www.notme.com">0</a></li>
          <li class="item-1b"><a href="http://www.notme.com">1</a></li>
          <li class="item-2b"><a href="http://www.findme.com" class="foo">2</a></li>
          <li class="item-3b"><a href="http://www.notme.com">3</a></li>
          <li class="item-4b"><a href="http://www.notme.com">4</a></li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii"><a href="http://www.notme.com">III</a></li>
</ul>
<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a"><a href="http://www.notme.com">A</a></li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1"><a href="http://www.notme.com">1</a></li>
          <li class="item-2"><a href="http://www.notme.com">2</a></li>
          <li class="item-3"><a href="http://www.notme.com">3</a></li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii"><a href="http://www.notme.com">III</a></li>
</ul>
<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a"><a href="http://www.notme.com">A</a></li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1"><a href="http://www.notme.com">1</a></li>
          <li class="item-2"><a href="http://www.notme.com">2</a></li>
          <li class="item-3"><a href="http://www.notme.com">3</a></li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii"><a href="http://www.notme.com">III</a></li>
</ul>
<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a"><a href="http://www.notme.com">A</a></li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1"><a href="http://www.notme.com">1</a></li>
          <li class="item-2"><a href="http://www.notme.com">2</a></li>
          <li class="item-3"><a href="http://www.notme.com">3</a></li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii"><a href="http://www.notme.com">III</a></li>
</ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
var $OuterStart = jQuery('#OuterStart');
var $InnerStart = jQuery('#StartHereA');
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
JQ Whole Page
var $x = $(".item-1a");
pending…
JQ w/Scope (Outer)
var $x = $OuterStart.find(".item-1a");
pending…
JQ w/Scope (Inner)
var $x = $InnerStart.find(".item-1a");
pending…
JS
var x = document.getElementsByClassName(".item-1a");
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