Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more isType functions improvements #663

Closed
FarSeeing opened this issue Jul 3, 2012 · 1 comment
Closed

Some more isType functions improvements #663

FarSeeing opened this issue Jul 3, 2012 · 1 comment
Labels

Comments

@FarSeeing
Copy link

Although you accepted my pull request not long ago, I am still not happy about some isType functions 😃

There are several variants, depending on the goal of the library itself and I'd like to see your guide for this.

  1. to minify code:
var objectTypes = ['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'];
if (!nativeIsArray)
{
      objectTypes.push('Array');
}
each(objectTypes , function(name) {
      _['is' + name] = function(obj) {
            return toString.call(obj) == '[object ' + name + ']';
      };
});

Maybe some more minifications.

  1. to maximize performance:
    move isFunction, isRegExp, isDate functions to their own declarations using typeof/instanceof:
_.isFunction = function(obj) {
      return typeof obj === 'function';
}
_.isRegExp = function(obj) {
      return obj instanceof RegExp;
}
_.isDate = function(obj) {
      return obj instanceof Date;
}

see http://jsperf.com/isfunction-performance and http://jsperf.com/isregexp-test-for-regexp for performance graphs

  1. to unify code branches:
each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Null', 'Undefined'], function(name) {
      _['is' + name] = function(obj) {
            return toString.call(obj) == '[object ' + name + ']';
      };
});
if (!_.isNull(null)) {
      _.isNull = function(obj) {
            return obj === null;
      };
}
if (!_.isUndefined()) {
      _.isUndefined = function(obj) {
            return obj === void 0;
      };
}
@jashkenas
Copy link
Owner

The #2 plan in this list won't fly -- because those tests won't work on objects from different frames ... but otherwise, feel free to send a pull request if you think you have an improvement on the current implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants