Array Unique

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var nav_array = ['0', '1', '1', '2', '3', '3', '3'];
};
</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
Comparison
function array_unique(arr) {
  var result = [],
      i;
  for (i = 0; i < arr.length; i++) {
    if (result.indexOf(arr[i]) === -1) {
      result.push(arr[i]);
    }
  }
  return result;
}

nav_array = array_unique(nav_array);
pending…
Hash
function array_unique(arr) {
  var obj = {},
      result = [],
      i;

  for (i = 0; i < arr.length; i++) {
    obj[i] = 1;
  }
  for (var key in obj) {
    result.push(key);
  }
  return result;
}

nav_array = array_unique(nav_array);
pending…
Compare (forEach)
function array_unique(arr) {
  var result = [],
      i;
  arr.forEach(function(elem) {
    if (result.indexOf(elem) === -1) {
      result.push(elem);
    }
  });
  return result;
}

nav_array = array_unique(nav_array);
pending…
Filtering
function array_unique(arr) {
  return arr.reverse().filter(

  function(e, i, arr) {
    return arr.indexOf(e, i + 1) === -1;
  }).reverse();
}

nav_array = array_unique(nav_array);
pending…
Filter (lastIndexOf)
function array_unique(arr) {
  return arr.filter(
    function(e, i, arr) {
      return arr.lastIndexOf(e) === i;
    });
}

nav_array = array_unique(nav_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