Alternative isFunction Implementations

JavaScript performance comparison

Revision 22 of this test case created by Qodeninja

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

  //Typeof

  function isFunctionD(object) {
   return (typeof object === "function")
  }

  function isFunctionS(object) {
     return Object.prototype.toString.call(object) == '[object Function]';
  }
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
isFunctionD
isFunctionD(Test);
isFunctionD(getClass);
isFunctionD(hasProperty);
!isFunctionD(expression);
pending…
"isFunctionS"
isFunctionS(Test);
isFunctionS(getClass);
isFunctionS(hasProperty);
!isFunctionS(expression);
pending…
isFunctionA
isFunctionA(Test);
isFunctionA(getClass);
isFunctionA(hasProperty);
!isFunctionA(expression);
pending…
isFunctionB
isFunctionB(Test);
isFunctionB(getClass);
isFunctionB(hasProperty);
!isFunctionB(expression);
pending…
isFunctionC
isFunctionC(Test);
isFunctionC(getClass);
isFunctionC(hasProperty);
!isFunctionC(expression);
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.

0 Comments