Find array in array

JavaScript performance comparison

Test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
hungryMind's method
var a2 =  [1,2,1,1,2,1,1,1,2,2];
var a1 = [1,1,2,2];
function find(arr1, arr2) {
    var len = 1
    var result = 0;
    var sep = "_"
    var s2 = arr2.join(sep);
    for (len=1;len <= a1.length; len++)
    {
        var aa1 = arr1.slice(0, len)
        var s1 = aa1.join(sep);
        if(s2.indexOf(s1)>=0){
            result = aa1.length;
        }
        else {
            break;
        }
    }
    return result;
}
find(a1, a2);
pending…
Shef's method
var haystack = [1,2,1,1,2,1,1,1,2,2],
    needle = [1,1,2,2];

// convert arrays to strings
var str_haystack = haystack.join(','),
    str_needle = needle.join(',');

// find first occurrence
var position = str_haystack.indexOf(str_needle);

// if it was present, then it's that + needle length
if(position > -1) position += needle.length;
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