Underscore.js isType Alternatives (v4)

Revision 4 of this benchmark created by Paul Miller 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() {
  var hasOwnProperty = {}.hasOwnProperty,
      toString = {}.toString.call;

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

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