Unique Array Elements

Benchmark created on


Setup

var array = [1,3,6,73,4,6,6,4,34,2,56,79,5,786,55143,4253,76,878,76354,54,4,543564,56473,456,34,3,2,3,4,67,7,4,4,2,5,7,9,0,8,7,5,43,32,3,5,56,6,9];
    
    var uniqueByReduce = function(a) {
        return a.reduce(function(p, c) {
            if (p.indexOf(c) < 0) p.push(c);
            return p;
        }, []);
    };
    
    var uniqueByIndexOf = function(a) {
        var unique = [];
        for (var i = 0; i < a.length; i++) {
            if (unique.indexOf(a[i]) == -1) {
                unique.push(a[i]);
            }
        }
        return unique;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Reduce
var unique = uniqueByReduce(array);
ready
IndexOf
var unique = uniqueByIndexOf(array);
ready

Revisions

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