unique in array (v8)

Revision 8 of this benchmark created by Arif Zaman on


Description

I have changed the size of the arrays to 1000, and the range of numbers to be from 0 to 100000, (but only multiples of 100) so that there are a number of matches.

The original example had a very small array, where efficiency is not so important, unless you are doing many arrays.

It appears that much of the code had not been tested, so there were a number of trivial errors. I have fixed them, and verified to see that the results are identical.

Preparation HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var size=1000;
    var arr = Array.apply(0,Array(size)).map(function(i,j){return Math.floor(Math.random()*size*100)});
    
    var arrclone1 = [], arrclone2 = [], arrclone3 = [];
    for (var i=0; i<size; i++)
        arrclone1[i] = arrclone2[i] = arrclone3[i] = arr[i];
    
    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.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;
    }
    
    Array.prototype.getUnique2 = function(){
       var u = Object.create(null), a = [];
       for(var i = 0, l = this.length; i < l; ++i){
          if(u[this[i]]) continue;
          a.push(this[i]);
          u[this[i]] = 1;
       }
       return a;
    }
    
    Array.prototype.getUnique3 = function(){
       var u = Object.create(null), a = [];
       for(var i = 0, l = this.length; i < l; ++i){
          if(this[i] in u) 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]);
            }
        }
        results.push(sorted_arr[sorted_arr.length - 1]);
        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
_.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
arr.getUnique2
arr.getUnique2();
ready
arr.getUnique3
arr.getUnique3();
ready

Revisions

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