Remove Duplicate Array Tests (v36)

Revision 36 of this benchmark created on


Preparation HTML

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

Setup

var array = [],
      j, n;
  for (n = 0; n < 30; n++)
  for (j = 0; j < 30; j++)
  array.push(j*n);

Test runner

Ready to run.

Testing in
TestOps/sec
Using each method
var temp = [];
$.each(array, function(i, el) {
  if ($.inArray(el, temp) === -1) temp.push(el);
});
return temp;
ready
Using for method #1
var temp = {};
for (var i = 0; i < array.length; i++) {
  temp[array[i]] = array[i];
}
c = [];
for (var key in temp) {
  c.push(key);
}
return c;
ready
Using for method #2
var temp = {};
for (var i = 0; i < array.length; i++)
temp[array[i]] = true;
var r = [];
for (var k in temp)
r.push(k);
return r;
ready
Using for object
var u = {},
    a = [],
    v;
for (var i = 0, l = array.length; i < l; ++i) {
  v = array[i];
  if (u.hasOwnProperty(v)) {
    continue;
  }
  a.push(v);
  u[v] = 1;
}
return a;
ready
Map
var temp = {},
    result = [],
    x;
for (var i = array.length; i >= 0; i--) {
  x = array[i];
  if (!(x in temp)) {
    temp[x] = 1;
    result.push(x);
  }
}
return result;
ready
sort & step
/*
b: sorted array,
i: current index,
l: last added item,
r: resulting list
start with the last item in b in the results list
for each item in b, starting from the 2nd from the end
  if it's not the same as the last added one, add it & update the last added
*/
for(var b=array.sort(), i=b.length, l=b[--i], r=[l]; i--;)
  if(b[i] !== l) r.push(l = b[i]);
return r;
ready
modified map
/* can skip the first item check & use the array for the 'in' */
for (var n = array.length, result = [array[n--]], i; n--;) {
  i = array[n];
  if (!(i in result)) result.push(i);
}
return result;
ready
Double While
var b;
var c;

b = array.length;
while (c = --b) while (c--) array[b] !== array[c] || array.splice(c, 1);
return array;
ready
Sort and filter
// order is not preserved in this case
var previous;

array.sort();

return array.filter(function(item) {
  var result = item !== previous;
  previous = item;
  return result;
});
ready
filter with indexOf
return array.filter(function(item, index, array) {
  return array.indexOf(item) >= index;
});
ready
filter with indexOf (keeping last occurrence)
// will keep only last occurrence
// i.e: [1, 2, 4, 2, 1] becomes [4, 2, 1]
return array.filter(function(item, index, array) {
  return array.indexOf(item, index + 1) === -1;
});
ready
map (+ forEach)
// don't work on IE8
var temp = {},
    result = [];

array.forEach(function(item) {
  if (!(item in temp)) {
    temp[item] = 1;
    result.push(item);
  }
});

return result;
ready
map (different key check)
var temp = {},
    result = [],
    x;
for (var i = array.length; i >= 0; i--) {
  x = array[i];
  if (!temp[x]) {
    temp[x] = true;
    result.push(x);
  }
}
return result;
ready
map (growing loop)
var temp = {},
    result = [],
    x;

for (var i = 0; i < array.length; i++) {
  x = array[i];

  if (!(x in temp)) {
    temp[x] = 1;
    result.push(x);
  }
}

return result;
ready
ES6 Set
return Array.from(new Set(array));
ready
map (using while and replaces pushes with indexing) -- reverses order
var indexes = {}
    , result = []
    , k = array.length
    , i = 0
    , v
    ;
while ( k-- ) {
  ( (v = array[k]) in indexes )
    || (result[i++] = (indexes[v] = v))
}
return result;
ready

Revisions

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