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
- Revision 5: published by Ezra Olubi
- Revision 6: published by Nikos M.
- Revision 7: published by Itsik Avidan
- Revision 8: published by me
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 15: published
- Revision 16: published
- Revision 17: published
- Revision 18: published
- Revision 19: published by ZAlexz
- Revision 20: published by dalisoft
- Revision 21: published by dalisoft
- Revision 22: published by Mario DE WEERD
0 comments
Comment form temporarily disabled.