coffee vs js. Array::unique2

Benchmark created by clux on


Preparation HTML

<script>
  Array.prototype.uniqueCoffee = function() {
    var k, value, _i, _len, _results;
    k = 0;
    _results = [];
    for (_i = 0, _len = this.length; _i < _len; _i++) {
      value = this[_i];
      if (!~this.indexOf(value, ++k)) {
        _results.push(value);
      }
    }
    return _results;
  };
  
  Array.prototype.unique = function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
coffee version
[5,5,5,4,4,3,2,1,1,2,3,4].uniqueCoffee();
ready
plain js
[5,5,5,4,4,3,2,1,1,2,3,4].unique();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.