instanceOf Array vs Array.isArray() (v20)

Revision 20 of this benchmark created by dalisoft on


Description

Comparison between the different ways of checking if a value is an array.

Setup

var A = [1, 2, 3, 4, 5],
    C = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]];
    function fnIsArr(arr){
    return Array.isArray(arr);
    }
    var isArr = Array.isArray;
    var Ar = Array;

Test runner

Ready to run.

Testing in
TestOps/sec
Instance of Array A
if (A instanceof Array) {
return true;
}
return false;
ready
IsArray A
if (Array.isArray(A)) {
return true;
}
return false;
ready
Constructor of A
if (A.constructor === Array) {
return true;
}
return false;
ready
Array-only method check
if (A.reduceRight) {
return true;
}
return false;
ready
Fn isArr
if (fnIsArr(A)) {
return true;
}
return false;
ready
cached isArray
if (isArr(A)) {
return true;
}
return false;
ready
Cached constructor
if (A.constructor === Ar) {
return true;
}
return false;
ready

Revisions

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