merge unique array

Benchmark created by Tester on


Preparation HTML

<script>
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'];
var b = ['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e'];
shuffle(a);
shuffle(b);



function arrayUnique(array) {
    var a = array.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }

    return a;
};
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
map
map = {};
a.map(function(i){map[i] = 1;});
b.map(function(i){map[i] = 1;});
var c = Object.keys(map);
ready
concat unique
var c = arrayUnique(a.concat(b));
ready
merge unique
for(var j = 0; j < b.length; j++){
    if(a.indexOf(b[j]) === -1) {
        a.push(b[j]);
    }
}
 
ready
concat filter
var c = a.concat(b);
var d = c.filter(function (item, pos) {return c.indexOf(item) === pos});
ready
concat unshift
var c = a.concat(b),
    length = c.length
    d = [];

while (length--) {
    var item = c[length];
    if (d.indexOf(item) === -1) {
        d.unshift(item);
    }
}
ready

Revisions

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

  • Revision 1: published by Tester on