Alternative isFunction Implementations

JavaScript performance comparison

Test case created by Kit Goncharov

Info

This test case compares three alternative isFunction implementations:

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.

Testing in unknown unknown
Test Ops/sec
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. Here’s a list of current revisions for this page:

0 comments

Add a comment