typeof

JavaScript performance comparison

Revision 4 of this test case created by Craig Patik

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 typeOf5 = function _typeOf5(o) {
        var reported, objType;
   
        // Null
        if (o === null) { return 'null'; }
   
        // Basic, non-object types
        reported = typeof o;
        if (/function|undefined|string|boolean|number/.test(reported)) { return reported; }
   
        // Objects
        if (reported === 'object') {
            // Check for special JavaScript types that inherit from Object
            objType = Object.prototype.toString.call(o);
   
            // Common types
            if (/^\[object\s(Array|Date|RegExp)\]$/.test(objType)) {
                return (/^\[object\s(Array|Date|RegExp)\]$/).exec(objType)[1];
            }
   
            // DOM element
            if (typeof HTMLElement === 'object' && o instanceof HTMLElement) { // DOM Level 2
                return 'element';
            }
            if (typeof o.nodeName === 'string' && o.nodeType === 1) {
                return 'element';
            }
   
            // DOM node
            if (typeof Node === 'object' && o instanceof Node) { // DOM Level 2
                return 'node';
            }
            if (typeof o.nodeType === 'number' && typeof o.nodeName === 'string') {
                return 'node';
            }
   
            // Node list
            if (/^\[object (HTMLCollection|NodeList|Object)\]$/.test(objType) &&
                    typeof o.length === 'number' &&
                    typeof o.item !== 'undefined' &&
                    (o.length === 0 || (typeof o[0] === 'object' && o[0].nodeType > 0))) {
                return 'nodelist';
            }
   
            // Some other type of object
            return reported;
        }
   
        return reported;
    };
   
    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…

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