jQuery index vs hasClass (v22)

Revision 22 of this benchmark created on


Description

Test which is faster, index compared to hasClass.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<section>
<article></article>
<article class="foo" data-foo="1"></article>
<article class="bar"></article>
<article></article>
</section>

<script>
  var found = false;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
hasClass
if ($("article").hasClass("foo")) {
 found = true;
}
ready
index
if ($("article").index() === 0) {
 found = true;
}
ready
data
if ($("article").data("foo") === 1) {
 found = true;
}
ready
attr
if ($("article").attr("data-foo") === '1') {
 found = true;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.