typeof

JavaScript performance comparison

Revision 5 of this test case created by LJHarb

Info

some bench about grabbing types

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var typeObj = {};
   
    function typeOf1(obj) {
      return typeObj.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
    }
   
    var re = /\s([a-zA-Z]+)/;
    function typeOf2(obj) {
      return typeObj.toString.call(obj).match(re)[1].toLowerCase();
    }
   
    var re2 = /\w+(?=])/;
    function typeOf3(obj) {
      return re2.test(typeObj.toString.call(obj)) &&
             RegExp.lastMatch.toLowerCase();
    }
   
    var typeOf4 = (function(Object, RegExp){
      // https://gist.github.com/WebReflection/6323064
      var toString = Object.prototype.toString,
          cache = (Object.create || Object)(null),
          matchClass = /\w+(?=])/;
      return function typeOf(Unknown) {
        var asString = typeof Unknown;
        return asString == 'object' ? (
          Unknown === null ? 'null' : (
            cache[asString = toString.call(Unknown)] || (
              cache[asString] = matchClass.test(asString) &&
                                RegExp.lastMatch.toLowerCase()
            )
          )
        ) : asString;
      };
    }(Object, RegExp));
   
    var toStrMap = {
      "[object Boolean]": "boolean",
      "[object Number]": "number",
      "[object String]": "string",
      "[object Function]": "function",
      "[object Array]": "array",
      "[object Date]": "date",
      "[object RegExp]": "regexp",
      "[object Object]": "object",
      "[object Error]": "error"
    };
    var typeOf5 = function (item) {
      return item == null ? String(item) : toStrMap[typeObj.toString.call(item)] || 'object';
    };
   
    var typeOf6 = function (item) {
      if (item === null) { return 'null'; }
      var type = typeof item;
      if (type !== 'object') { return type; }
      else {
        return typeObj.toString.call(item).slice(8, -1).toLowerCase();
      }
    };
   
    var typeOf7 = function (item) {
      if (item === null) { return 'null'; }
      var type = typeof item;
      if (type !== 'object') { return type; }
      else {
        var str = typeObj.toString.call(item);
        return str.slice(str.indexOf(' ') + 1, -1).toLowerCase();
      }
    };
   
    var result;
};
</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
typeOf1
for(var
  i = 0,
  list = [
    i, null, undefined, false, '',
    {}, new Date, Function
  ],
  length = list.push(list);
  i < length; result = typeOf1(list[i++])
);
pending…
typeOf2
for(var
  i = 0,
  list = [
    i, null, undefined, false, '',
    {}, new Date, Function
  ],
  length = list.push(list);
  i < length; result = typeOf2(list[i++])
);
pending…
typeOf3
for(var
  i = 0,
  list = [
    i, null, undefined, false, '',
    {}, new Date, Function
  ],
  length = list.push(list);
  i < length; result = typeOf3(list[i++])
);
pending…
typeOf4
for(var
  i = 0,
  list = [
    i, null, undefined, false, '',
    {}, new Date, Function
  ],
  length = list.push(list);
  i < length; result = typeOf4(list[i++])
);
pending…
typeOf5
for(var
  i = 0,
  list = [
    i, null, undefined, false, '',
    {}, new Date, Function
  ],
  length = list.push(list);
  i < length; result = typeOf5(list[i++])
);
pending…
typeOf6
for(var
  i = 0,
  list = [
    i, null, undefined, false, '',
    {}, new Date, Function
  ],
  length = list.push(list);
  i < length; result = typeOf6(list[i++])
);
pending…
typeOf7
for(var
  i = 0,
  list = [
    i, null, undefined, false, '',
    {}, new Date, Function
  ],
  length = list.push(list);
  i < length; result = typeOf7(list[i++])
);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment