Unique array merging - concat vs looping

JavaScript performance comparison

Test case created by

Preparation code

<script>
s1 = createArr(100);
s2 = createArr(200);
b1 = createArr(10000);
b2 = createArr(20000);
function createArr(length) {
   var arr = [];
   for(i=0; i<length; i++) {
    arr[i] = (i%2) ? i.toString() : i; // Uses toString() to try and avoid any engine specific optimisations on pure integer arrays.
   }
   return arr;
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  function l(i,x) {
     h = {}
     n = []
     for (a = 2; a--; i=x)
        i.map(function(b){
          h[b] = h[b] || n.push(b)
        })
     return n
  }
  
  function c(i,x) {
     h = {}
     n = []
        i.concat(x).map(function(b){
          h[b] = h[b] || n.push(b)
        })
     return n
  }
  
  

};
</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
Looping - Small
l(s1,s2);
pending…
Concat - Small
c(s1,s2);
pending…
Looping - Large
l(b1,b2);
pending…
Concat - Large
c(b2,b2);
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