Underscore.js isType Alternatives (v10)

Revision 10 of this benchmark created 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));
</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
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
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

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