instanceOf Array vs Array.isArray()

JavaScript performance comparison

Revision 8 of this test case created by me

Info

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

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    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;
};
</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
Instance of Array A
A instanceof Array ? true : false;
pending…
IsArray A
isArray(A) ? true : false;
pending…
Constructor of A
A && A.constructor === Array ? true : false;
pending…
toString A
toString.call(A) === '[object Array]'
pending…
Instance of Array B
B instanceof Array ? true : false;
pending…
IsArray B
isArray(B) ? true : false;
pending…
Constructor of B
B && B.constructor === Array ? true : false;
pending…
toString B
toString.call(B) === '[object Array]'
pending…
Instance of Array C
C instanceof Array ? true : false;
pending…
IsArray C
isArray(C) ? true : false;
pending…
Constructor of C
C && C.constructor === Array ? true : false;
pending…
toString C
toString.call(C) === '[object Array]'
pending…
Instance of Array D
D instanceof Array ? true : false;
pending…
IsArray D
isArray(D) ? true : false;
pending…
Constructor of D
D && D.constructor === Array ? true : false;
pending…
toString D
toString.call(D) === '[object Array]'
pending…
Instance of Array E
E instanceof Array ? true : false;
pending…
IsArray E
isArray(E) ? true : false;
pending…
Constructor of E
E && E.constructor === Array ? true : false;
pending…
toString E
toString.call(E) === '[object Array]'
pending…
Instance of Array F
F instanceof Array ? true : false;
pending…
IsArray F
isArray(F) ? true : false;
pending…
Constructor of F
F && F.constructor === Array ? true : false;
pending…
toString F
toString.call(F) === '[object Array]'
pending…
Instance of Array G
G instanceof Array ? true : false;
pending…
IsArray G
isArray(G) ? true : false;
pending…
Constructor of G
G && G.constructor === Array ? true : false;
pending…
toString G
toString.call(G) === '[object Array]'
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

Comment form temporarily disabled.

Add a comment