unique in array v2 (v18)

Revision 18 of this benchmark created on


Preparation HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script>var underscore = _.noConflict();</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.compat.min.js"></script>
<script>var lodash = _.noConflict();</script>

Setup

var arr = [0, 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
      0, 1, 2, 3, 4, 0, 1, 3, 5, 0, 7, 1, 'f', 'h', 'c', 'a', 'b', 0, 1, 5
    ];
    
    var arrclone1 = [0, 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
      0, 1, 2, 3, 4, 0, 1, 3, 5, 0, 7, 1, 'f', 'h', 'c', 'a', 'b', 0, 1, 5
    ];
    var arrclone2 = [0, 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
      0, 1, 2, 3, 4, 0, 1, 3, 5, 0, 7, 1, 'f', 'h', 'c', 'a', 'b', 0, 1, 5
    ];
    var arrclone3 = [0, 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
      0, 1, 2, 3, 4, 0, 1, 3, 5, 0, 7, 1, 'f', 'h', 'c', 'a', 'b', 0, 1, 5
    ];
    
    var jqFilter = function(v, k) {
      return $.inArray(v, arr) === k;
    }
    
    Array.prototype.unique = function() {
      var a = [];
      for (var i = 0, l = this.length; i < l; i++)
        if (a.indexOf(this[i]) === -1)
          a.push(this[i]);
      return a;
    }
    
    Array.prototype.unique2 = function() {
      var a = [];
      var l = this.length;
      while (--l > 0) {
        if (a.indexOf(this[l]) === -1)
          a.push(this[l]);
      }
      return a;
    }
    
    Array.prototype.unique2rev = function() {
      var a = [];
      var l = arr.length;
      while (--l > 0) {
        if (a.indexOf(arr[l]) === -1)
          a.push(arr[l]);
      }
      a.sort(function(a, b) {
        return a - b
      });
      return a;
    }
    
    Array.prototype.getUnique = function() {
      var u = {}, a = [];
      for (var i = 0, l = this.length; i < l; ++i) {
        if (u.hasOwnProperty(this[i])) {
          continue;
        }
        a.push(this[i]);
        u[this[i]] = 1;
      }
      return a;
    }
    
    function onlyUnique(value, index, self) {
      return self.indexOf(value) === index;
    }
    
    Array.prototype.sortFilter = function() {
      var sorted_arr = this.sort();
      var results = [];
      for (var i = 0; i < this.length - 1; i++) {
        if (sorted_arr[i + 1] == sorted_arr[i]) {
          results.push(sorted_arr[i]);
        }
      }
      return results;
    }
    
    Array.prototype.sortUnique = function() {
      this.sort();
      var last_i;
      for (var i = 0; i < this.length; i++)
        if ((last_i = this.lastIndexOf(this[i])) !== i)
          this.splice(i + 1, last_i - i);
      return this;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery grep
$.grep(arr, jqFilter);
ready
Underscore uniq
underscore.uniq(arr);
ready
My arr.unique
arr.unique();
ready
Rafael's arr.getUnique
arr.getUnique();
ready
TLindig's filter
arrclone1.filter(onlyUnique);
ready
swilliams' sorted testing
arrclone2.sortFilter();
ready
My version of sorted testing
arrclone3.sortUnique();
ready
Sorting my arr.unique
arr.unique().sort();
ready
Lodash uniq
lodash.uniq(arr);
ready
My arr.unique while
arr.unique2();
ready
My version of sorted testing + while
arr.unique2rev();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.