instanceOf Array vs Array.isArray() vs toString (v6)

Revision 6 of this benchmark created by Nikos M. 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]];

Test runner

Ready to run.

Testing in
TestOps/sec
Instance of Array A
A instanceof Array ? true : false;
ready
Instance of Array B
B instanceof Array ? true : false;
ready
Instance of Array C
C instanceof Array ? true : false;
ready
IsArray A
Array.isArray(A) ? true : false;
ready
IsArray B
Array.isArray(B) ? true : false;
ready
IsArray C
Array.isArray(C) ? true : false;
ready
Constructor of A
A.constructor === Array ? true : false;
ready
Constructor of B
B.constructor === Array ? true : false;
ready
Constructor of C
C.constructor === Array ? true : false;
ready
ToString.call of A
toString.call(A) === '[object Array]' ? true : false
ready
ToString.call of B
toString.call(B) === '[object Array]' ? true : false
ready
ToString.call of C
toString.call(C) === '[object Array]' ? true : false
ready
A.push && A.pop
A.push && A.pop ? true : false
ready
B.push && B.pop
B.push && B.pop ? true : false
ready
C.push && C.pop
C.push && C.pop ? true : false
ready
A.prototype.push
A.__proto__.push ? true : false
ready
B.prototype.push
B.__proto__.push ? true : false
ready
C.prototype.push
C.__proto__.push ? true : false
ready

Revisions

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