$(this).attr('id') vs this.id

JavaScript performance comparison

Revision 12 of this test case created

Preparation code

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

<a id="tester1" class="tests" href="#">abcdefghijkl</a>
<a id="tester2" class="tests" href="#">abcdefghijkl</a>
<a id="tester3" class="tests" href="#">abcdefghijkl</a>
<a id="tester4" class="tests" href="#">abcdefghijkl</a>
<a id="tester5" class="tests" href="#">abcdefghijkl</a>
<a id="tester6" class="tests" href="#">abcdefghijkl</a>
<a id="tester7" class="tests" href="#">abcdefghijkl</a>
<a id="tester8" class="tests" href="#">abcdefghijkl</a>
<a id="tester9" class="tests" href="#">abcdefghijkl</a>
<a id="tester10" class="tests" href="#">abcdefghijkl</a>
<a id="tester11" class="tests" href="#">abcdefghijkl</a>
<a id="tester12" class="tests" href="#">abcdefghijkl</a>
<a id="tester13" class="tests" href="#">abcdefghijkl</a>
<a id="tester14" class="tests" href="#">abcdefghijkl</a>
<a id="tester15" class="tests" href="#">abcdefghijkl</a>
<a id="tester16" class="tests" href="#">abcdefghijkl</a>

<script>
  $a = $('.tests');
</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
$(this).attr('id');
$a.each(function(index){
     $t = $(this).attr('id');
});
pending…
$(this).id
$a.each(function(index){
     $t = this.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:

1 comment

Manel commented :

I think the second case is incorrect on the Chart. It would be this.id and not $(this).id

Add a comment