instanceOf Array vs Array.isArray()

JavaScript performance comparison

Revision 11 of this test case created

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]];
};
</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…
Instance of Array B
B instanceof Array ? true : false;
pending…
Instance of Array C
C instanceof Array ? true : false;
pending…
IsArray A
Array.isArray(A) ? true : false;
pending…
IsArray B
Array.isArray(B) ? true : false;
pending…
IsArray C
Array.isArray(C) ? true : false;
pending…
Constructor of A
A.constructor === Array ? true : false;
pending…
Constructor of B
B.constructor === Array ? true : false;
pending…
Constructor of C
C.constructor === Array ? true : false;
pending…
jQuery A
toString.call(A) === "[object Array]" ? true : false;
pending…
jQuery B
toString.call(B) === "[object Array]" ? true : false;
pending…
jQuery C
toString.call(C) === "[object Array]" ? true : false;
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