jQuery lookup in array or as variable first

JavaScript performance comparison

Test case created by Rich

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<p>hidden content</p>
<ul id="ulist" style="display:none;">
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
<li>i</li>
<li>j</li>
<li>k</li>
<li>l</li>
<li>m</li>
<li>n</li>
<li>o</li>
<li>p</li>
<li>q</li>
<li>r</li>
<li>s</li>
<li>t</li>
<li>u</li>
<li>v</li>
<li>w</li>
<li>x</li>
<li>y</li>
<li>z</li>
<li>aa</li>
<li>bb</li>
<li>cc</li>
<li>dd</li>
<li>ee</li>
<li>ff</li>
<li>gg</li>
<li>hh</li>
<li>ii</li>
<li>jj</li>
</ul>
<script>
  var array1 = [],
      array2 = [];
</script>
    

Preparation code output

<p>hidden content</p> <ul id="ulist" style="display:none;"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> <li>g</li> <li>h</li> <li>i</li> <li>j</li> <li>k</li> <li>l</li> <li>m</li> <li>n</li> <li>o</li> <li>p</li> <li>q</li> <li>r</li> <li>s</li> <li>t</li> <li>u</li> <li>v</li> <li>w</li> <li>x</li> <li>y</li> <li>z</li> <li>aa</li> <li>bb</li> <li>cc</li> <li>dd</li> <li>ee</li> <li>ff</li> <li>gg</li> <li>hh</li> <li>ii</li> <li>jj</li> </ul> <script> var array1 = [], array2 = []; </script>

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
loop with jquery reference
for (var _x = 0, len = $('#ulist > li').length; _x < len; _x++) {
 array1.push($('#ulist > li')[_x]);
}
pending…
loop with variable reference
var liArray = $('#ulist > li');
for (var _x = 0, len = liArray.length; _x < len; _x++) {
 array2.push(liArray[_x]);
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments

hidden content