monomorphic vs polymorphic

JavaScript performance comparison

Test case created by benjamingr

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var Magician = {};
   
    function Rabbit(message){
        this.toString = function(){
            return message;
        }
    }
   
    Magician.pullString = function(str){
        return new Rabbit(str.length);
    }
    Magician.pullObj = function(objWithA){
        return new Rabbit(objWithA.a);
    }
    Magician.pullRabbit= function(anything){
        if(anything instanceof String){
            return new Rabbit(str.length);
        }else if(anything instanceof Object && anything.a){
            return new Rabbit(anything.a);
        }
    }
};
</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
monomorphic
Magician.pullString("Hello");
Magician.pullObj({a:5});
 
pending…
polymorphic
Magician.pullRabbit("Hello");
Magician.pullRabbit({a:5});
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