Underscore.js isType Alternatives (v15)

Revision 15 of this benchmark created by Jesús Germade on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://documentcloud.github.com/underscore/underscore.js"></script>
<script>
(function(window) {
  var hasOwnProperty = {}.hasOwnProperty,
      toString = {}.toString;

  _.mixin({
    // Is a given variable an arguments object?
    isArgumentsNew: toString.call(arguments) == '[object Arguments]'
      ? function(obj) {
          return toString.call(obj) == '[object Arguments]';
        }
      : function(obj) {
         return !!(obj && hasOwnProperty.call(obj, 'callee'));
        },
    
    // Is a given value a function?
    isFunctionNew: function(obj) {
      return toString.call(obj) == '[object Function]';
    },
    
    // Is a given value a string?
    isStringNew: function(obj) {
      return toString.call(obj) == '[object String]';
    },
    
    // Is a given value a number?
    isNumberNew: function(obj) {
      return toString.call(obj) == '[object Number]';
    },
    
    // Is a given value a date?
    isDateNew: function(obj) {
      return toString.call(obj) == '[object Date]';
    },
    
    // Is the given value a regular expression?
    isRegExpNew: function(obj) {
      return toString.call(obj) == '[object RegExp]';
    }
  });

window._typeof = {

    // Is a given value a function?
    isFunction: function(obj) {
      return typeof obj == 'function';
    },
    
    // Is a given value a string?
    isString: function(obj) {
      return typeof obj == 'string';
    },
    
    // Is a given value a number?
    isNumber: function(obj) {
      return typeof obj == 'number';
    },

    // Is a given value a date?
    isDate: function(obj) {
      return obj instanceof Date;
    },
    
    // Is the given value a regular expression?
    isRegExp: function(obj) {
      return obj instanceof RegExp;
    }
};

window._constructor = {

    // Is a given value a function?
    isFunction: function(obj) {
      return obj ? obj.constructor.name === 'Number' : false;
    },
    
    // Is a given value a string?
    isString: function(obj) {
      return obj ? obj.constructor.name === 'String' : false;
    },
    
    // Is a given value a number?
    isNumber: function(obj) {
      return obj ? obj.constructor.name === 'Number' : false;
    },

    // Is a given value a date?
    isDate: function(obj) {
      return obj ? obj.constructor.name === 'Date' : false;
    },
    
    // Is the given value a regular expression?
    isRegExp: function(obj) {
      return obj ? obj.constructor.name === 'RegExp' : false;
    }
};
}(window));

window._instance = (function () {
    function _isType (type) {
        return function (o) {
            return (typeof o === type);
        };
    }

    function _instanceOf (_constructor) {
        return function (o) {
            return ( o instanceof _constructor );
        };
    }

    return {
        isFunction: _instanceOf(Function),
        isString: _isType('string'),
        isNumber: _instanceOf(Number),
        isDate: _instanceOf(Date),
        isRegExp: _instanceOf(RegExp)
    };
})();

window._curry = (function () {

    function _isType (curryType) {
        function checkType(type) {
            return (type === curryType)
        }
        return function (o) {
            return checkType(typeof o);
        };
    }

    function _instanceOf (_constructor) {
        return function (o) {
            return ( o instanceof _constructor );
        };
    }

    return {
        isFunction: _isType('function'),
        isString: _isType('string'),
        isNumber: _isType('number'),
        isDate: _instanceOf(Date),
        isRegExp: _instanceOf(RegExp)
    };
})();


</script>

Setup

var $ = window.jQuery,
      _ = window._,
      array = [1, 2, 3],
      func  = function(){},
      string = "string",
      number = 101,
      date   = new Date,
      regexp = /regexp/i;

Test runner

Ready to run.

Testing in
TestOps/sec
Proposed Implementation
_.isArgumentsNew(arguments);
_.isArgumentsNew(array);

_.isFunctionNew(func);
_.isFunctionNew(array);

_.isStringNew(string);
_.isStringNew(array);

_.isNumberNew(number);
_.isNumberNew(string);

_.isDateNew(date);
_.isDateNew(array);

_.isRegExpNew(regexp);
_.isRegExpNew(func);
ready
Sanity Check
_.isArguments(arguments);
_.isArguments(array);

$.type(func) == 'function';
$.type(array) == 'function';

$.type(string) == 'string';
$.type(array) == 'string';

$.type(number) == 'number';
$.type(string) == 'number';

$.type(date) == 'date';
$.type(array) == 'date';

$.type(regexp) == 'regexp';
$.type(func) == 'regexp';
ready
typeof
_.isArguments(arguments);
_.isArguments(array);

_typeof.isFunction(func);
_typeof.isFunction(array);

_typeof.isString(string);
_typeof.isString(array);

_typeof.isNumber(number);
_typeof.isNumber(string);

_typeof.isDate(date);
_typeof.isDate(array);

_typeof.isRegExp(regexp);
_typeof.isRegExp(func);
ready
constructor.name
_.isArguments(arguments);
_.isArguments(array);

_constructor.isFunction(func);
_constructor.isFunction(array);

_constructor.isString(string);
_constructor.isString(array);

_constructor.isNumber(number);
_constructor.isNumber(string);

_constructor.isDate(date);
_constructor.isDate(array);

_constructor.isRegExp(regexp);
_constructor.isRegExp(func);
ready
instanceof
_.isArguments(arguments);
_.isArguments(array);

_instance.isFunction(func);
_instance.isFunction(array);

_instance.isString(string);
_instance.isString(array);

_instance.isNumber(number);
_instance.isNumber(string);

_instance.isDate(date);
_instance.isDate(array);

_instance.isRegExp(regexp);
_instance.isRegExp(func);
ready
curry
_.isArguments(arguments);
_.isArguments(array);

_curry.isFunction(func);
_curry.isFunction(array);

_curry.isString(string);
_curry.isString(array);

_curry.isNumber(number);
_curry.isNumber(string);

_curry.isDate(date);
_curry.isDate(array);

_curry.isRegExp(regexp);
_curry.isRegExp(func);
ready
Current Implementation
_.isArguments(arguments);
_.isArguments(array);

_.isFunction(func);
_.isFunction(array);

_.isString(string);
_.isString(array);

_.isNumber(number);
_.isNumber(string);

_.isDate(date);
_.isDate(array);

_.isRegExp(regexp);
_.isRegExp(func);
ready

Revisions

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

  • Revision 1: published by Jeremy Ashkenas on
  • Revision 4: published by Paul Miller on
  • Revision 5: published by John-David Dalton on
  • Revision 7: published by Shane O'Sullivan on
  • Revision 9: published on
  • Revision 10: published on
  • Revision 11: published by Jesús Germade on
  • Revision 12: published by test on
  • Revision 13: published by Dan on
  • Revision 14: published by Jesús Germade on
  • Revision 15: published by Jesús Germade on
  • Revision 16: published by Jesús Germade on
  • Revision 17: published by mingc on
  • Revision 18: published by Eldar Abusalimov on