isArray - duck typing vs. instanceof
JavaScript performance comparison
Info
This test is motivated by the use of duck typing in the current underscore.js implementation for _.isType() functions.
I believe we should not care about the cross frame edge case - take a look at the performance.
Preparation code
<script>
var obj0 = [1, 2, 3],
obj1 = {
a: 5,
b: 7
},
obj2 = [
[
[]
]
],
obj3 = 8,
toString = Object.prototype.toString;
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
duck typing |
|
pending… |
instanceof |
|
pending… |
secure cross frame |
|
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 2: published by Andi
- Revision 4: published
- Revision 5: published
1 comment
This is exactly the benchmark I was looking for. The answer to this has alluded me for some time.