uniq2

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    Array.prototype.uniq = function() {
        var o = {}, i, l = this.length, r = [];
        for(i=0; i<l;i+=1) o[this[i]] = this[i];
        for(i in o) r.push(o[i]);
        return r;
    };
    Array.prototype.unique = function() {
        var a = [], l = this.length;
        for(var i=0; i<l; i++) {
            for(var j=i+1; j<l; j++)
                if (this[i] === this[j]) j = ++i;
            a.push(this[i]);
        }
        return a;
    };
    function uniqFor(array) {
     var i, j;
     array = array.slice();
     for (i = 0; i < array.length; i += 1) {
       for (j = i + 1; j < array.length; j += 1) {
         if (array[i] === array[j]) {
           array.splice(j, 1);
           j -= 1;
         }
       }
      }
      return array;
    }
    function uniqFilter(array) {
      return array.filter(function(value, i) {
        return array.indexOf(value, i + 1) === -1;
      });
    }
   
    var c=[];
    for (var i=0;i<1000;i++){
      c[i]=Math.floor((Math.random()*1000));
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
f1
c.uniq();
pending…
f2
c.unique()
pending…
uniqFor
uniqFor(c)
pending…
uniqFilter
uniqFilter(c)
pending…
uniqueArray1
uniqueArray1 = c.filter(function(elem, pos) {
    return c.indexOf(elem) == pos;
})
pending…
uniqueArray2
uniqueArray2 = c.filter(function(elem, pos, self) {
    return self.indexOf(elem) == pos;
})
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. Here’s a list of current revisions for this page:

0 comments

Add a comment