mootools array uniq vs vanilla

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.1/mootools-yui-compressed.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    MooTools.More={version:"1.4.0.1",build:"a4244edf2aa97ac8a196fc96082dd35af1abab87"};(function(a){Array.implement({min:function(){return Math.min.apply(null,this);
    },max:function(){return Math.max.apply(null,this);},average:function(){return this.length?this.sum()/this.length:0;},sum:function(){var b=0,c=this.length;
    if(c){while(c--){b+=this[c];}}return b;},
        unique2: function(){
                  var h = {}, ret = [], arrLen = this.length, next=0, el, type, needToCheckTypes = {undefined: 1, boolean: 1, number: 1, string: 1};
                  while (arrLen--) {
                      el = this[arrLen];
                      type = typeof el;
                      if((needToCheckTypes[typeof el] || el === null) && h[el]) continue;
                      ret[next++] = el;
                      h[el] = 1;
                  }
              return ret;      
        },
   
    unique:function(){return[].combine(this);},shuffle:function(){for(var c=this.length;c&&--c;){var b=this[c],d=Math.floor(Math.random()*(c+1));
    this[c]=this[d];this[d]=b;}return this;},reduce:function(d,e){for(var c=0,b=this.length;c<b;c++){if(c in this){e=e===a?this[c]:d.call(null,e,this[c],c,this);
    }}return e;},reduceRight:function(c,d){var b=this.length;while(b--){if(b in this){d=d===a?this[b]:c.call(null,d,this[b],b,this);}}return d;}});})();
    var mixedArrayWithObjects = [];
    for (var i in window) {
      if (typeOf(window[i] === 'object'))
      mixedArrayWithObjects.push(window[i]);
    }
   
    var arr = [];
       
        for (var i = 0; i < 1000; i++) {
          arr.push(i);
       
          if (i > 0 && i % 10 == 0) {
            arr.push(i - 10);
          }
        }
       
        var arr1 = ['q', 'w', 'e', 'r', 't', 'q', 'w', 'e', 'r', 't', 'p', 'y', 'i', 'o', 'u', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 'p', 'y', 'i', 'w', 'e', 'r', 't', 'q', 'w', 'e', 'r', 't', 'p', 'y', 'i', 'o', 'u', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 'p', 'y', 'i', 'o', 'u', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a'];
       
        var arr2 = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", "Alabama", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];
   
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Original number array
arr.unique();
pending…
Original short string array
arr1.unique();
pending…
Original long string array
arr2.unique();
pending…
Original object array
mixedArrayWithObjects.unique();
pending…
New number array
arr.unique2();
pending…
new short string array
arr1.unique2();
pending…
new long string array
arr2.unique2();
pending…
New object array
mixedArrayWithObjects.unique2();
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