Type based dispatch with/without wrapping

Benchmark created on


Setup

var MyType = function MyType() { }
    
    var obj_dispatch = {}
    obj_dispatch[String] = function(str) { return str; }
    String.prototype.runFn = function() { return this; }
    obj_dispatch[MyType] = function(obj) { return obj; }
    MyType.prototype.runFn = function() { return this; }
    
    function runFn(obj) {
      if (typeof(obj) === "string") {
        return obj_dispatch[String](obj);
      } else {
        return obj_dispatch[obj.constructor](obj);
      }
    }
    
    
    var prim = "Hello";
    var str = new String("Hello");
    
    var my_obj = new MyType();

Test runner

Ready to run.

Testing in
TestOps/sec
primitive dispatch
runFn(prim)
ready
wrapped dispatch
runFn(str)
ready
native method
str.runFn()
ready
object dispatch
runFn(my_obj)
ready
native method
my_obj.runFn()
ready

Revisions

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