instanceOf Array vs Array.isArray() (v2)

Revision 2 of this benchmark created by Artur Raczyński on


Description

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

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

Setup

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);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
instanceof testArray
testArray instanceof Array ? true : false;
ready
instanceof testObject
testObject instanceof Array ? true : false;
ready
instanceof testString
testString instanceof Array ? true : false;
ready
instanceof testNumber
testNumber instanceof Array ? true : false;
ready
instanceof testBool
testBool instanceof Array ? true : false;
ready
instanceof testUndefined
testUndefined instanceof Array ? true : false;
ready
constructor == testArray
testArray.constructor === Array ? true : false;

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

Revisions

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