typeof-test-case

JavaScript performance comparison

Test case created by Aymeric

Info

Three different fonctions for typeOf

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script>
  function typeOf_match(obj){return({}).toString.call(obj).match(/\s(\w+)/)[1]}
 
  function typeOf_jquery(obj){
    var c2t={"[object Boolean]":"Boolean","[object Number]":"Number","[object String]":"String","[object Function]":"Function","[object Array]":"Array","[object Date]":"Date","[object RegExp]":"RegExp","[object Object]":"Object"};
    return obj==null?String(obj):c2t[({}).toString.call(obj)]||"object";
  }
 
  function typeOf_slice(obj) {return({}).toString.call(obj).slice(8,-1)}
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Match
typeOf_match(new Number(2));
typeOf_match(new Date());
typeOf_match("test");
typeOf_match(null);
typeOf_match(/a/);
typeOf_match([]);
typeOf_match(function(){});
pending…
jQuery (clone)
typeOf_jquery(new Number(2));
typeOf_jquery(new Date());
typeOf_jquery("test");
typeOf_jquery(null);
typeOf_jquery(/a/);
typeOf_jquery([]);
typeOf_jquery(function(){});
pending…
jQuery (original)
jQuery.type(new Number(2));
jQuery.type(new Date());
jQuery.type("test");
jQuery.type(null);
jQuery.type(/a/);
jQuery.type([]);
jQuery.type(function(){});
pending…
Slice
typeOf_slice(new Number(2));
typeOf_slice(new Date());
typeOf_slice("test");
typeOf_slice(null);
typeOf_slice(/a/);
typeOf_slice([]);
typeOf_slice(function(){});
pending…

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

Compare results of other browsers

0 comments

Add a comment