compare array unique algorithms

JavaScript performance comparison

Revision 15 of this test case created

Preparation code

<script>
var utils = {
uniq: function(arr) {
    var temp = {}, l = arr.length, result = [];
    while(l) !temp[arr[--l].id] && result.unshift(arr[l]) && (temp[arr[l].id] = 1);
    return result;
  },

  uniqDouble: function(arr) {
    var temp = {}, l = arr.length, result = [];
    while(l) temp[arr[--l].id] = arr[l];
    for (var k in temp) result.push(temp[k]);
    return result;
  }
}
</script>
<script>
Benchmark.prototype.setup = function() {
    var array = [
      { id: 1 },
      { id: 2 },
      { id: 3 },
      { id: 4 },
      { id: 5 },
      { id: 6 },
      { id: 7 },
      { id: 8 },
      { id: 9 },
      { id: 9 },
      { id: 10 },
      { id: 1, new: true },
      { id: 2, new: true },
      { id: 3, new: true }
    ]
};
</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
single loop
utils.uniq(array);
pending…
double loop
utils.uniqDouble(array);
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