jQuery hasClass vs $(document).data() (v25)

Revision 25 of this benchmark created by Alex on


Description

Test which is faster for checking a flag: hasClass() or checking a data() attribute.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class="foo"></div>
<script>
  var found = false;
  $("body").addClass("foobar").data('blarg',true).attr("data-foo", "bar");
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
data
if ($("body").data("blarg")) {
 found = true;
}
ready
hasClass
if ($("body").hasClass("foobar")) {
 found = true;
}
ready
if($("body[data-foo]")) {
  found = true;
}
ready

Revisions

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