$(this) or collection-faking

JavaScript performance comparison

Test case created by Kyle Simpson and last updated

Preparation code

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

<style>
.foo { color:blue; }
.other { color:red; }
</style>


<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>
<p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p>
<p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>

<script>
  var $a = $(0);
  
  function fn_1() {
   var $a = $(this);
   if ($a.attr("rel") == "foo") {
    $a.addClass("foo");
   }
   else {
    $a.addClass("other");
   }
  }
  
  function fn_2() {
   $a.context = $a[0] = this; // fake the collection object
   if ($a.attr("rel") == "foo") {
    $a.addClass("foo");
   }
   else {
    $a.addClass("other");
   }
  }
</script>
    

Preparation code output

<style> .foo { color:blue; } .other { color:red; } </style> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p> <p><a href="#">link</a></p><p>blah</p><p><a href="#" rel="foo">link</a></p><p>blah</p> <p><a href="#" rel="bar">link</a></p><p>blah</p><p><a href="#" rel="wee">link</a></p><p>blah</p>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
$(this)
$("a").each(fn_1);
pending…
$a.context = $a[0] = this
$("a").each(fn_2);
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.

0 Comments

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah

link

blah