Node list iteration

JavaScript performance comparison

Test case created

Info

Comparing two different styles of iterating through node lists.

Preparation code

<div class="ancestor">
  <div class="interesting">1</div>
  <div class="not-interesting"></div>
  <div class="interesting">2</div>
  <div class="also-not-interesting"></div>
  <div class="interesting">3</div>
  <div class="interesting">4</div>
  <div class="also-not-interesting"></div>
  <div class="also-not-interesting"></div>
  <div class="container">
    <div class="not-interesting"></div>
     <div class="interesting">5</div>
     <div class="interesting">6</div>
     <div class="also-not-interesting"></div>
     <div class="subcontainer">
       <div class="interesting">7</div>
       <div class="also-not-interesting"></div>
       <div class="interesting">8</div>
     </div>
   </div>
 
<script>
Benchmark.prototype.setup = function() {
    var ancestor = document.querySelector('.ancestor');
    var interestingNodes = ancestor.querySelectorAll('.interesting');
    var result = [];
};
</script>

Preparation code output

1
2
3
4
5
6
7
8

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Length iteration
for (var i = 0; i < interestingNodes.length; i++) {
  var interestingNode = interestingNodes[i];
  result.push(interestingNode);
}
pending…
Truthy iteration
for (var i = 0, interestingNode; interestingNode = interestingNodes[i]; i++) {
  result.push(interestingNode);
}
pending…
Cached length iteration
var len = interestingNodes.length;
for (var i = 0; i < len; i++) {
  var interestingNode = interestingNodes[i];
  result.push(interestingNode);
}
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

Add a comment