Alternative isFunction Implementations
JavaScript performance comparison
Info
This test case compares three alternative isFunction implementations:
isFunctionAchecks the internal[[Class]]name of an object to determine if it is a function. Unfortunately, while this check is the most reliable, it's also less efficient due to the extra call toObject#toString.isFunctionBwas proposed as an alternative to thetypeofandinstanceofoperators by Garrett Smith. This implementation uses duck-typing, so it may produce incorrect results.isFunctionCis currently used in Underscore.js. This implementation uses only basic duck-typing; thus, it is the most likely to produce incorrect results.
Preparation code
<script>
var getClass = {}.toString,
hasProperty = {}.hasOwnProperty,
expression = /Kit/g;
function Test() {}
// Checks the internal [[Class]] name of the object.
function isFunctionA(object) {
return object && getClass.call(object) == '[object Function]';
}
// Partial duck-typing implementation by Garrett Smith.
function isFunctionB(object) {
if (typeof object != 'function') return false;
var parent = object.constructor && object.constructor.prototype;
return parent && hasProperty.call(parent, 'call');
}
// Pure duck-typing implementation taken from Underscore.js.
function isFunctionC(object) {
return !!(object && object.constructor && object.call && object.apply);
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
isFunctionA |
|
pending… |
isFunctionB |
|
pending… |
isFunctionC |
|
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 Kit Goncharov
- Revision 2: published
- Revision 3: published
- Revision 4: published by Joel Purra
- Revision 6: published
- Revision 7: published
- Revision 8: published by Max
- Revision 9: published by Max
- Revision 10: published
- Revision 11: published by Burakkuhatto
- Revision 12: published by Burakkuhatto
- Revision 13: published
0 comments