JS Dedupe Many Dupes

JavaScript performance comparison

Test case created by Chris Moschini

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  //oldIE polyfill for Array..indexOf
  if (![].indexOf) {
      Array.prototype.indexOf = function (o) {
          for (var i = this.length - 1; i >= 0 && this[i] != o; i--)
          { }
          return i;
      }
  }
  
  var longArray = [];
  for(var i = 0; i < 10000; i++) {
    longArray.push(Math.floor(Math.random() * 1000));
  }
  
  function dedupeSmall(array) {
  	var a = [];
  	$.each(array, function(i, o) {
  		if (a.indexOf(o) < 0)
  			a.push(o);
  	});
  
  	return a;
  }
      
  function dedupeLarge(array) {
  	array.sort();
  	for(var i = 1; i < array.length; i++) {
  		if (array[i-1] == array[i])
  			array.splice(i--, 1);
  	}
  	return array;
  }
  
  function dedupeManyDupes(array) {
  	array.sort();
    var l = array.length;
  	for(var i = 1; i < l; i++) {
  		if (array[i-1] == array[i]) {
  			var dupe = array[i];
  			var mark = i;
  			for(i++; i < l && array[i] == dupe; i++)
  			{}
  			var snip = i - mark;
  			array.splice(mark, snip);
  			i -= snip;
  			l -= snip;
  		}
  	}
  	return array;
  }

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Small Large
dedupeSmall(longArray);
pending…
Large Large
dedupeLarge(longArray);
pending…
ManyDupes Large
dedupeManyDupes(longArray);
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.

0 Comments