Attribute vs Class selection

JavaScript performance comparison

Revision 7 of this test case created

Info

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

Preparation code

<ul>
<li id="1a" data-cargroup="abcde" class="a b c d e">A</li>
<li id="2b" data-cargroup="bcdea" class="b c d e a>B</li>
<li id="3c" data-cargroup="cdeab" class="c d e a b">C</li>
<li id="4d" data-cargroup="deabc" class="d e a b c">C</li>
<li id="5e" data-cargroup="eabcd" class="e a b c d">D</li>
<li id="1f" data-cargroup="abcde" class="a b c d e">A</li>
<li id="2g" data-cargroup="bcdea" class="b c d e a>B</li>
<li id="3h" data-cargroup="cdeab" class="c d e a b">C</li>
<li id="4i" data-cargroup="deabc" class="d e a b c">C</li>
<li id="5j" data-cargroup="eabcd" class="e a b c d">D</li>
<li id="1z" data-cargroup="abcde" class="a b c d e">A</li>
<li id="2k" data-cargroup="bcdea" class="b c d e a>B</li>
<li id="3l" data-cargroup="cdeab" class="c d e a b">C</li>
<li id="4m" data-cargroup="deabc" class="d e a b c">C</li>
<li id="5n" data-cargroup="eabcd" class="e a b c d">D</li>
<li id="1o" data-cargroup="abcde" class="a b c d e">A</li>
<li id="2p" data-cargroup="bcdea" class="b c d e a>B</li>
<li id="3q" data-cargroup="cdeab" class="c d e a b">C</li>
<li id="4r" data-cargroup="deabc" class="d e a b c">C</li>
<li id="5s" data-cargroup="eabcd" class="e a b c d">D</li>
<li id="1t" data-cargroup="abcde" class="a b c de">A</li>
<li id="2u" data-cargroup="bcdea" class="b c d e a>B</li>
<li id="3v" data-cargroup="cdeab" class="c d e a b">C</li>
<li id="4w" data-cargroup="deabc" class="d e a b c">C</li>
<li id="5x" data-cargroup="eabcd" class="e a b c d">D</li>
</ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Preparation code output

  • A
  • C
  • C
  • D
  • A
  • C
  • C
  • D
  • A
  • C
  • C
  • D
  • A
  • C
  • C
  • D
  • A
  • 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
Data-attribute selection
$('[data-cargroup]');
pending…
Class-based selection
$('.c');
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