Attribute vs Class selection

JavaScript performance comparison

Revision 19 of this test case created

Info

Tests speed of selection for data attribute selection versus class-based selection.

Preparation code

<div class="a1" id="parent">
  <div class="a1">
    <div class="a1">
      <div class="a1">
        <div class="a1">
          <div class="a1">
            <div class="a1">
              <ul id="list">
                <li id="1" data-cargroup="A" class="js-CarGroup">
                  A
                </li>
                <li id="2" data-cargroup="B" class="js-CarGroup">
                  B
                </li>
                <li id="3" data-cargroup="C" class="js-CarGroup">
                  C
                </li>
                <li id="4" data-cargroup="C" class="js-CarGroup">
                  C
                </li>
                <li id="5" data-cargroup="D" class="js-CarGroup">
                  D
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var listElement = $('#list')
};
</script>

Preparation code output

  • A
  • B
  • C
  • C
  • D

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
class selection
$('.js-CarGroup');
pending…
ID selection
$('#list .js-CarGroup');
pending…
ID context
$('.js-CarGroup', '#list')
pending…
Element context
$('.js-CarGroup', listElement)
pending…
Data Attribute
$('li[data-cargroup]')
pending…
Data Attribute Element Context
$('li[data-cargroup]', listElement)
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