Mine vs yours

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var _ = {};
    _.is = function(obj, type) {
      if (!type) {
        return Object.prototype.toString.call(obj);
      } else {
        var temp = type.toLowerCase();
        type = temp === 'regexp' ? 'RegExp' : temp.replace(/./, function(a) {
          return a.toUpperCase();
        }); // prevent string errors
        return Object.prototype.toString.call(obj) === '[object ' + type + ']';
      }
    }
    _.isArguments = function(obj) {
      return this.is(obj, 'Arguments');
    };
    _.isArray = function(obj) {
      return this.is(obj, 'Array');
    };
    _.isBool = function(obj) {
      return this.is(obj, 'Boolean');
    };
    _.isDate = function(obj) {
      return this.is(obj, 'Date') || obj instanceof Date;
    };
    _.isElement = function(obj) {
      return obj ? obj.nodeType === 1 : false;
    };
    _.isFalsy = function(obj) {
      return !obj;
    };
    _.isInfinite = function(obj) {
      return obj === Infinity || obj === -Infinity;
    };
    _.isFunction = function(obj) {
      return this.is(obj, 'Function');
    };
    _.isNaN = function(obj) {
      return typeof obj === "number" && obj !== obj;
    };
    _.isNull = function(obj) {
      return this.is(obj, 'Null');
    };
    _.isNumber = function(obj) {
      return this.is(obj, 'number');
    };
    _.isObject = function(obj) {
      return typeof obj === "object";
    };
    _.isPlainObject = function(obj) {
      return typeof obj === "object" && _.is(obj, 'Object');
    };
    _.isRegExp = function(obj) {
      return this.is(obj, 'RegExp') || obj instanceof RegExp;
    };
    _.isString = function(obj) {
      return typeof obj === "string" && this.is(obj, 'String');
    };
    _.isUndefined = function(obj) {
      return typeof obj === "undefined";
    };
    var mine = Object.create(_);
    delete _;
    var _ = {};
    _.isArguments = function(obj) {
      return {}.toString.call(obj) === "[object Arguments]";
    };
   
    _.isArray = function(obj) {
      return {}.toString.call(obj) === "[object Array]";
    };
   
    _.isBool = function(obj) {
      return {}.toString.call(obj) === "[object Boolean]";
    };
   
    _.isDate = function(obj) {
      return {}.toString.call(obj) === "[object Date]" || obj instanceof Date;
    };
   
    _.isElement = function(obj) {
      return obj ? obj.nodeType === 1 : false;
    };
   
    _.isEqual = function(obj1, obj2) {
      var type = _.type(obj1),
          o;
   
      // Not the same TYPE
      if (type != _.type(obj2)) return false;
   
      // A primitive (all but arrays, objects (including null), nan, and HTML elements)
      if (typeof obj1 != 'object' && type != 'function' && type != 'nan' || type == 'null') return obj1 === obj2;
   
      // A Function
      if (type == 'function') {
        return obj1.toString() == obj2.toString() && _.isEqual(_.keys(obj1), _.keys(obj2)) && _.isEqual(_.values(obj1), _.values(obj2));
      }
   
      // NaN
      if (type == 'nan') return obj1 !== obj2;
   
      // An Object (array, object, or HTML element)
      if (typeof obj1 == 'object') {
        if (_.len(obj1) == 0) {
          return type == _.type(obj2) && _.len(obj2) == 0;
        } else {
          for (o in obj1) {
            return _.isEqual(obj1[o], obj2[o]);
          }
        }
      }
    };
   
    _.isFalsy = function(obj) {
      return (_.isUndefined(obj) || _.isNull(obj) || _.isNaN(obj) || obj === "" || obj === 0 || (_.isBool(obj) && Boolean(obj) === false));
    };
   
    _.isInfinite = function(obj) {
      return obj === Infinity || obj === -Infinity;
    };
   
    _.isFunction = function(obj) {
      return {}.toString.call(obj) === "[object Function]";
    };
   
    _.isNaN = function(obj) {
      return typeof obj === "number" && obj !== obj;
    };
   
    _.isNull = function(obj) {
      return {}.toString.call(obj) === "[object Null]";
    };
   
    _.isNumber = function(obj) {
      return {}.toString.call(obj) === "[object Number]";
    };
   
    _.isObject = function(obj) {
      return typeof obj === "object";
    };
   
    _.isPlainObject = function(obj) {
      return typeof obj === "object" && {}.toString.call(obj) === "[object Object]";
    };
   
    _.isRegExp = function(obj) {
      return {}.toString.call(obj) === "[object RegExp]" || obj instanceof RegExp;
    };
   
    _.isString = function(obj) {
      return typeof obj === "string" && {}.toString.call(obj) === "[object String]";
    };
   
    _.isUndefined = function(obj) {
      return typeof obj === "undefined";
    };
    var types = [{}, [], 'oi', 10];
    var names = ['Object', 'Array', 'String', 'Number'];
};
</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
Mine
for (var i = 0, len = names.length; i < len; i++) {
  mine['is' + names[i]](types[i]);
}
pending…
Yours
for (var i = 0, len = names.length; i < len; i++) {
  _['is' + names[i]](types[i]);
}
pending…

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

Compare results of other browsers

0 comments

Add a comment