Remove Duplicate Array Tests (v10)

Revision 10 of this benchmark created by imma 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

Revisions

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