jQuery Selector Case 1

JavaScript performance comparison

Revision 2 of this test case created by Cong Liu

Preparation code

<ul id="list">
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
</ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    function test1() {
      $('ul#list li');
    }
   
    function test2() {
      $('ul#list').find('li');
    }
   
    function test3() {
      $('li', '#list');
    }
   
    function test4() {
      $('#list').find('li');
    }
   
    function bench(func) {
      for (var i = 0; i < 500; i++) func();
    }
};
</script>

Preparation code output

  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item
  • Item

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
$('ul#list li')
bench(test1)
pending…
$('ul#list).find('li')
bench(test2)
pending…
$('li', '#list')
bench(test3)
pending…
$('#list').find('li')
bench(test4)
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