instanceOf Array vs Array.isArray() (v8)

Revision 8 of this benchmark created by me on


Description

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

Setup

var A = [1, 2, 3, 4, 5],
      B = ['a', 'b', 'c', 'd', 'e'],
      C = [
        [1, 2],
        [3, 4],
        [5, 6],
        [7, 8],
        [9, 10]
      ],
      D = 16,
      E = { foo: 'bar' },
      F = true,
      G = null;
    
    var isArray = Array.isArray,
        toString = Object.prototype.toString;

Test runner

Ready to run.

Testing in
TestOps/sec
Instance of Array A
A instanceof Array ? true : false;
ready
IsArray A
isArray(A) ? true : false;
ready
Constructor of A
A && A.constructor === Array ? true : false;
ready
toString A
toString.call(A) === '[object Array]'
ready
Instance of Array B
B instanceof Array ? true : false;
ready
IsArray B
isArray(B) ? true : false;
ready
Constructor of B
B && B.constructor === Array ? true : false;
ready
toString B
toString.call(B) === '[object Array]'
ready
Instance of Array C
C instanceof Array ? true : false;
ready
IsArray C
isArray(C) ? true : false;
ready
Constructor of C
C && C.constructor === Array ? true : false;
ready
toString C
toString.call(C) === '[object Array]'
ready
Instance of Array D
D instanceof Array ? true : false;
ready
IsArray D
isArray(D) ? true : false;
ready
Constructor of D
D && D.constructor === Array ? true : false;
ready
toString D
toString.call(D) === '[object Array]'
ready
Instance of Array E
E instanceof Array ? true : false;
ready
IsArray E
isArray(E) ? true : false;
ready
Constructor of E
E && E.constructor === Array ? true : false;
ready
toString E
toString.call(E) === '[object Array]'
ready
Instance of Array F
F instanceof Array ? true : false;
ready
IsArray F
isArray(F) ? true : false;
ready
Constructor of F
F && F.constructor === Array ? true : false;
ready
toString F
toString.call(F) === '[object Array]'
ready
Instance of Array G
G instanceof Array ? true : false;
ready
IsArray G
isArray(G) ? true : false;
ready
Constructor of G
G && G.constructor === Array ? true : false;
ready
toString G
toString.call(G) === '[object Array]'
ready

Revisions

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