Remove Duplicate Array Tests

JavaScript performance comparison

Revision 11 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js">
</script>
 
<script>
Benchmark.prototype.setup = function() {
    var array = [
      "sample1",
      "sample1",
      "sample1",
      "sample2",
      "sample2",
      "sample2",
      "sample1",
      "sample2",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample1",
      "sample1",
      "sample1",
      "sample1",
      "sample2",
      "sample2",
      "sample2",
      "sample1",
      "sample2",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample1",
      "sample1",
      "sample1",
      "sample1",
      "sample2",
      "sample2",
      "sample2",
      "sample1",
      "sample2",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample1",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample2",
      "sample1",
      "sample3"
    ]
};
</script>

Preparation code output

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
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 [1,2,3].filter
return array.filter(function(elem, pos) {
  return array.indexOf(elem) == pos;
})
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
*/

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…
opec dupe
 var i,
   len = array.length,
   outArray = [],
   obj = {};

 for (i = 0; i < len; i++) {
   obj[array[i]] = 0;
 }
 for (i in obj) {
   outArray.push(i);
 }
 return outArray;
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:

0 comments

Add a comment