JS Dedupe Small

JavaScript performance comparison

Test case created by Chris Moschini

Preparation code

<script src="//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 small = [ 10, 10, 5, 10, 4, 3, 4, 1 ];
   
    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 unknown unknown
Test Ops/sec
Small Small
dedupeSmall(small);
pending…
Large Small
dedupeLarge(small);
pending…
ManyDupes Small
dedupeManyDupes(small);
pending…

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

Compare results of other browsers

0 comments

Add a comment