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

Revision 20 of this benchmark created on


Description

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

Preparation HTML

<script src="https://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);
</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

Revisions

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