Compare Array.unique versions

JavaScript performance comparison

Test case created by Phil Glanville

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  Array.prototype.unique1 = function () {
    var a = this, b = [], c, i = a.length;
    again: while ( i-- ) {
      c = a[i];
      k = i; while( k-- ){ if (a[k] === c){ continue again; } }
      b.unshift( a[i] );
    }
    return b;
  }
  
  Array.prototype.unique2 = function () {
   u = [];
   for (var i = 0, l = this.length; i < l; i++) {
     if(u.indexOf(this[i]) == -1){
       u.push(this[i]);
     }
   }
   return u;
  }
  
  Array.prototype.unique3 = function () {
    var a = this, b = [], c, i = a.length;
    while ( i-- ) {
      c = a[i];
      if ( b.indexOf( c ) == -1 ) {
        b.push( c );
      } 
    }
    return b;
  }
  
  var test_array = [], i = 3000;
  while ( i-- ) {
    test_array.push( Math.round(Math.random() * 10) );
  }

};
</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
unique1
test_array.unique1();
pending…
unique2
test_array.unique2();
pending…
unique3
test_array.unique3();
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