typeString vs toType

Benchmark created on


Preparation HTML

<script>
  var toString = {}.toString;
  
  function typeString (v) {
      return v == null ? String(v) : toString.call(v).slice(8, -1);
  }
  
  function toType(obj) {
    return obj == null ? String(obj) : ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1]
  }
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
typeString
typeString(null);
typeString(undefined);
typeString({});
typeString("");
typeString(2);
 
ready
toType
toType(null);
toType(undefined);
toType({});
toType("");
toType(2);
 
ready

Revisions

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