instanceOf Array vs Array.isArray()

JavaScript performance comparison

Revision 21 of this test case created by dalisoft

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],
    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;
    Array.prototype.isArr = true;
};
</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
if (A instanceof Array) {
return true;
}
return false;
pending…
IsArray A
if (Array.isArray(A)) {
return true;
}
return false;
pending…
Constructor of A
if (A.constructor === Array) {
return true;
}
return false;
pending…
Array-only method check
if (A.reduceRight) {
return true;
}
return false;
pending…
Fn isArr
if (fnIsArr(A)) {
return true;
}
return false;
pending…
cached isArray
if (isArr(A)) {
return true;
}
return false;
pending…
Cached constructor
if (A.constructor === Ar) {
return true;
}
return false;
pending…
Array.prototype.isArr
if (A.isArr) {
return true;
}
return 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