jquery attr() vs getAttribute()

JavaScript performance comparison

Test case created by Sanghee

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="section1">
   <span class="top">top1</span>
   <div class="middle">
                middle1
                <span class="inner">inner1</span>
   </div>
   <div class="bottom">bottom1</div>
</div>
<div id="section2">
   <span class="top">top2</span>
   <div class="middle">
                middle2
                <span class="inner">inner2</span>
   </div>
   <div class="bottom">bottom2</div>
</div>
<script>
var elem = $("#section2");
</script>

Preparation code output

top1
middle1 inner1
bottom1
top2
middle2 inner2
bottom2

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
attr
elem.attr("id");
pending…
getAttribute
elem[0].getAttribute("id");
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