Remove Duplicate Array Tests

JavaScript performance comparison

Revision 12 of this test case created by Junxi

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var array = ['data-control="title"', 'data-control="title"', 'data-control="aa"', 'data-control="title"', 'data-control="flip"', 'data-control="aa"', 'data-control="flip"', 'data-control="title"', 'data-control="flip"', 'data-control="title"', 'data-control="title"', 'data-control="title"', 'data-control="title"', 'data-control="title"', 'data-control="title"', 'data-control="aa"', 'data-control="aa"', 'data-control="aa"'];
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Using each method
var temp = [];
$.each(array, function(i, el) {
  if ($.inArray(el, temp) === -1) temp.push(el);
});
return temp;
pending…
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;
pending…
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;
pending…
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;
pending…
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;
pending…
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
*/


function removeDuplicates(array) {
  var b = array.sort(),
    i = b.length,
    l, r;
  for (; i--;) {
    l = b[--i];
    r = [l];
    if (b[i] !== l) r.push(l = b[i]);
  }
  return r;
}
removeDuplicates(array);
pending…
remodifiied map
/* can skip the first item check & use the array for the 'in' */
var n = array.length,
  i, result;

for (; n--;) {
  result = [array[n--]];
  i = array[n];
  if (!(i in result)) result.push(i);
}
return result;
pending…
Array Proto
Array.prototype.removeDuplicates = function() {
  var temp = new Array();
  this.sort();
  for (i = 0; i < this.length; i++) {
    if (this[i] == this[i + 1]) {
      continue
    }
    temp[temp.length] = this[i];
  }
  return temp;
}
array.removeDuplicates();
pending…
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;
pending…
remodified map in array prototype
Array.prototype.removeDuplicates = function() {
  var n = array.length,
    i, result;

  for (; n--;) {
    result = [array[n--]];
    i = array[n];
    if (!(i in result)) result.push(i);
  }
  return result;
}
array.removeDuplicates();
pending…
remodified in function
function removeDuplicates(array) {
  var n = array.length,
    i, result;

  for (; n--;) {
    result = [array[n--]];
    i = array[n];
    if (!(i in result)) result.push(i);
  }
  return result;
}
removeDuplicates(array);
pending…
remodified with while
/* can skip the first item check & use the array for the 'in' */
var n = array.length,
  i, result;

while (n--) {
  result = [array[n--]];
  i = array[n];
  if (!(i in result)) result.push(i);
}
return result;
pending…
sort2
        function remove(array) {
          var array = array.sort(),
            currentIndex = array.length,
            lastAdded = array[--currentIndex],
            result = [lastAdded];
          for (; currentIndex--;)
            if (array[currentIndex] !== lastAdded) result.push(lastAdded = array[currentIndex]);
          return result;
        }
        remove(array);
pending…
sort3
        function remove(array) {
          var array = array.sort(),
            currentIndex = array.length,
            lastAdded = array[--currentIndex],
            result = [lastAdded];
          for (; currentIndex--;) {
            (array[currentIndex] !== lastAdded) && (result.push(lastAdded = array[currentIndex]));
          }
          return result;
        }
        remove(array);
pending…
sort4
/*
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;
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Russell Dempsey commented :

I didn't check all of these tests, but there are a few I did look at do not actually do anything.

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;

does not do anything except copy the array... what exactly are you trying to test here?

Add a comment