typeof

JavaScript performance comparison

Revision 3 of this test case created by Andrea Giammarchi

Info

some bench about grabbing types

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var typeOf1 = (function(Object, RegExp){
      // WTFPL License - http://en.wikipedia.org/wiki/WTFPL
      var
        toString = Object.prototype.toString,
        cache = (Object.create || Object)(null)
      ;
      return function typeOf(Unknown) {
        var asString = typeof Unknown;
        return asString == 'object' ? (
          Unknown === null ? 'null' : (
            cache[asString = toString.call(Unknown)] || (
              cache[asString] = asString.slice(8, -1).toLowerCase()
            )
          )
        ) : asString;
      };
    }(Object, RegExp));
   
    var typeOf2 = (function(Object, RegExp){
      // WTFPL License - http://en.wikipedia.org/wiki/WTFPL
      var toString = Object.prototype.toString,
          cache = (Object.create || Object)(null);
      return function typeOf(Unknown) {
        var asString = typeof Unknown;
        return asString == 'object' ? (
          Unknown === null ? 'null' : (
            cache[asString = toString.call(Unknown)] || (
              cache[asString] = asString
                .slice(asString.indexOf(' ') + 1, -1)
                .toLowerCase()
            )
          )
        ) : asString;
      };
    }(Object, RegExp));
   
    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…

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