instanceOf Array vs Array.isArray()

JavaScript performance comparison

Revision 2 of this test case created by Artur Raczyński

Info

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

Changed so that it also checks non-arrays for "array-ness"

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var testArray = [1, 'hi', false, 3400.5, [1, 2, 3], {'b': 42}],
        testObject = {'a': 15.1, 'beta': true, 3: 'hello', true: undefined},
        testString = 'I am a string',
        testNumber = 150.1,
        testBool = true,
        testUndefined = undefined;
   
    function instanceofArray(suspect) {
        return (suspect instanceof Array);
    }
   
    function constructorArray(suspect) {
        return (suspect.constructor === Array);
    }
   
    function isArrayArray(suspect) {
        return Array.isArray(suspect);
    }
};
</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
instanceof testArray
testArray instanceof Array ? true : false;
pending…
instanceof testObject
testObject instanceof Array ? true : false;
pending…
instanceof testString
testString instanceof Array ? true : false;
pending…
instanceof testNumber
testNumber instanceof Array ? true : false;
pending…
instanceof testBool
testBool instanceof Array ? true : false;
pending…
instanceof testUndefined
testUndefined instanceof Array ? true : false;
pending…
constructor == testArray
testArray.constructor === Array ? true : false;

 
pending…
constructor == testObject
testObject.constructor === Array ? true : false;
pending…
constructor == testString
testString.constructor === Array ? true : false;
pending…
constructor == testNumber
testNumber.constructor === Array ? true : false;
pending…
constructor == testBool
testBool.constructor === Array ? true : false;
pending…
isArray testArray
Array.isArray(testArray) ? true : false;
pending…
isArray testObject
Array.isArray(testObject) ? true : false;
pending…
isArray testString
Array.isArray(testString) ? true : false;
pending…
isArray testNumber
Array.isArray(testNumber) ? true : false;
pending…
isArray testBool
Array.isArray(testBool) ? true : false;
pending…
isArray testUndefined
Array.isArray(testUndefined) ? true : false;
pending…
"instanceof" summary
testArray instanceof Array ? true : false;
testObject instanceof Array ? true : false;
testString instanceof Array ? true : false;
testNumber instanceof Array ? true : false;
testBool instanceof Array ? true : false;
pending…
"constructor ==" summary
testArray.constructor === Array ? true : false;
testObject.constructor === Array ? true : false;
testString.constructor === Array ? true : false;
testNumber.constructor === Array ? true : false;
testBool.constructor === Array ? true : false;
pending…
"isArray" summary
Array.isArray(testArray) ? true : false;
Array.isArray(testObject) ? true : false;
Array.isArray(testString) ? true : false;
Array.isArray(testNumber) ? true : false;
Array.isArray(testBool) ? true : false;
pending…
wrapped instanceof
instanceofArray(testArray);
instanceofArray(testObject);
instanceofArray(testString);
instanceofArray(testNumber);
instanceofArray(testBool);
pending…
wrapped constructor
constructorArray(testArray);
constructorArray(testObject);
constructorArray(testString);
constructorArray(testNumber);
constructorArray(testBool);
pending…
wrapped isArray
isArrayArray(testArray);
isArrayArray(testObject);
isArrayArray(testString);
isArrayArray(testNumber);
isArrayArray(testBool);
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