In All Arrays

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // construct 15 arrays with random values
    var data = [];
    var results = [];
    (function() {
        var item, dups, val;
        var lenArray = 1000;
        var dataMax = lenArray * 1.2;
        for (var i = 0; i < 15; i++) {
            item = [];
            dups = {};
            for (var j = 0; j < lenArray; j++) {
                do {
                    val = "a" + Math.floor(Math.random() * dataMax);
                } while (dups[val]);
                dups[val] = true;
                item.push(val);
            }
            data.push(item);
        }
    })();
   
    function containsAll(/* pass all arrays here */) {
        var output = [];
        var cntObj = {};
        var array, item, cnt;
        for (var i = 0; i < arguments.length; i++) {
            array = arguments[i];
            unique = {};
            for (var j = 0; j < array.length; j++) {
                item = array[j];
                cnt = cntObj[item] || 0;
                // if cnt is exactly the number of previous arrays,
                // then increment by one so we count only one per array
                if (cnt == i) {
                    cntObj[item] = cnt + 1;
                }
            }
        }
        // now collect all results that are in all arrays
        for (item in cntObj) {
            if (cntObj[item] === arguments.length) {
                output.push(item);
            }
        }
        return(output);
    }    
   
    function containsAll2(input) {
        var result = input[0].filter(function(v) {
            return input.slice(1).every(function(a) {
                return a.indexOf(v) !== -1;
            });
        });
        return(result);
    }
};
</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
ES5 search
containsAll2(data);
pending…
Collect in Object
containsAll.apply(window, data);
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