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

JavaScript performance comparison

Revision 7 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

Konr Ness commented :

I was just curious if it mattered if the HTML was valid, since the original test had all of the elements having the same ID (technical invalid). Turns out, that made no difference.

Add a comment