Alternative isFunction Implementations (v29)

Revision 29 of this benchmark created by Asok on


Preparation HTML

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

  function isFunctionD(object) {
   return typeof(object) === 'function';
  }

  function isFunctionDC(object) {
   if (typeof(obj) != 'function') return false;
   return !!(obj && obj.constructor && obj.call && obj.apply);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
isFunctionA
isFunctionA(Test);
isFunctionA(getClass);
isFunctionA(hasProperty);
!isFunctionA(expression);
ready
isFunctionB
isFunctionB(Test);
isFunctionB(getClass);
isFunctionB(hasProperty);
!isFunctionB(expression);
ready
isFunctionC
isFunctionC(Test);
isFunctionC(getClass);
isFunctionC(hasProperty);
!isFunctionC(expression);
ready
isFunctionD
isFunctionD(Test);
isFunctionD(getClass);
isFunctionD(hasProperty);
!isFunctionD(expression);
ready
isFunctionDC
isFunctionDC(Test);
isFunctionDC(getClass);
isFunctionDC(hasProperty);
!isFunctionDC(expression);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.