instanceOf Array vs Array.isArray()
JavaScript performance comparison
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.
Test | Ops/sec | |
---|---|---|
instanceof testArray |
|
pending… |
instanceof testObject |
|
pending… |
instanceof testString |
|
pending… |
instanceof testNumber |
|
pending… |
instanceof testBool |
|
pending… |
instanceof testUndefined |
|
pending… |
constructor == testArray |
|
pending… |
constructor == testObject |
|
pending… |
constructor == testString |
|
pending… |
constructor == testNumber |
|
pending… |
constructor == testBool |
|
pending… |
isArray testArray |
|
pending… |
isArray testObject |
|
pending… |
isArray testString |
|
pending… |
isArray testNumber |
|
pending… |
isArray testBool |
|
pending… |
isArray testUndefined |
|
pending… |
"instanceof" summary |
|
pending… |
"constructor ==" summary |
|
pending… |
"isArray" summary |
|
pending… |
wrapped instanceof |
|
pending… |
wrapped constructor |
|
pending… |
wrapped isArray |
|
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:
- Revision 1: published by Brian Jemilo II
- Revision 2: published by Artur Raczyński
- Revision 3: published
- Revision 4: published
0 comments