Remove Duplicate Array Tests

JavaScript performance comparison

Revision 5 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 = [],
        j, n;
    for (n = 0; n < 1; n++)
    for (j = 0; j < 1000; j++)
    array.push(j);
};
</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…

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