array vs qsa

JavaScript performance comparison

Test case created by RobG

Preparation code

<div class="A"></div>
<div class="B"></div>
<div class="C"></div>
<div class="A"></div>
<div class="B"></div>
<div class="C"></div>
<div class="A"></div>
<div class="B"></div>
<div class="C"></div>
<div class="A"></div>
<div class="B"></div>
<div class="C"></div>
      
<script>
Benchmark.prototype.setup = function() {
  function useArrayMethods() {
    var a = document.getElementsByClassName('A');
    var b = document.getElementsByClassName('B');
    var c = Array.prototype.slice.call(a).concat(Array.prototype.slice.call(b));
    c.forEach(function(i){c[i]});
  }
  
  function useQSA() {
    var a = document.querySelectorAll('.A, .B');
    for (var i=0, iLen=a.length; i<iLen; i++) {
      a[i];
    } 
  }
  

};
</script>

Preparation code output

<div class="A"></div> <div class="B"></div> <div class="C"></div> <div class="A"></div> <div class="B"></div> <div class="C"></div> <div class="A"></div> <div class="B"></div> <div class="C"></div> <div class="A"></div> <div class="B"></div> <div class="C"></div>

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
slice/concat with forEach
useArrayMethods();
pending…
qSA with for loop
useQSA()
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