typeString vs toType (v3)

Revision 3 of this 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]
  }


function has(type, v) {
    return toString.call(v).toLowerCase().substr(8, -1) === type.toLowerCase();
}
  
</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
has
has('null', null);
has('undefined', undefined);
has('object', {});
has('string', "");
has('number', 2);
 
ready

Revisions

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