jQuery versus className

JavaScript performance comparison

Test case created by JC Fant

Info

A quick test to see the performance based on using jQuery's .attr('class') versus the native className;

Preparation code

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



<div class="wrapper">
     <div class="innerWrapper">
          <div id="getMe" class="button">
               <a href="javascript:void(0)" id="button" class="buttonRef">
                    <span class="buttonText">Text</span>
               </a>
          </div>
     </div>
</div>
<script>
  var jElem = $("#getMe");
  var dElem = document.getElementById("getMe");
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
1. jQuery - get Element + get Class
$("#getMe").attr('class');
pending…
2. GET DOM ELEM + get Class
document.getElementById("getMe").className;
pending…
3. jQuery get Class
jElem.attr('class');
pending…
4. DOM get class
dElem.className;
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