toType optimizations

JavaScript performance comparison

Test case created by benekastah

Info

Trying to improve toType function from this article.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var toString = Object.prototype.toString;
   
    var toTypeOrig = function(obj) {
        return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
        }
       
       
       
    var toTypeOrigCompat = function(obj) {
        return obj == null ? '' + obj : ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
        }
       
       
       
    var toTypeProto = function(obj) {
        return toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
        }
       
       
       
    var toTypeProtoCompat = function(obj) {
        return obj == null ? '' + obj : toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
        }
};
</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
toTypeOrig
var toType = toTypeOrig

toType(null)
toType(undefined)
toType(5)
toType({})
toType([])
toType(alert)
pending…
toTypeOrigCompat
var toType = toTypeOrigCompat

toType(null)
toType(undefined)
toType(5)
toType({})
toType([])
toType(alert)
pending…
toTypeProto
var toType = toTypeProto

toType(null)
toType(undefined)
toType(5)
toType({})
toType([])
toType(alert)
pending…
toTypeProtoCompat
var toType = toTypeProtoCompat

toType(null)
toType(undefined)
toType(5)
toType({})
toType([])
toType(alert)
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