bind-vs-reference

Benchmark created by WebReflection on


Description

I've always been curios about bound contextes against lookup

Setup

// forced native
    Function.prototype.$bind = function () {
      var self = this;
      return function bound() {
        return self.call(self);
      };
    };
    
    // native where present
    "bind" in Function.prototype || (Function.prototype.bind = Function.prototype.$bind);
    
    // helper
    function set(obj, value) {
      if (Object.defineProperty) {
        Object.defineProperty(obj, "value", {get:value});
      } else if (obj.__defineGetter__) {
        obj.__defineGetter__("value", value);
      } else {
        // do not consider in IE < 9
        obj.value = function () {};
      }
    }
    
    var bound = function () {
        return this.value;
    };
    bound.value = Math.random();
    
    var native = bound.bind(bound);
    
    var lookup = function () {
        return bound.value;
    };
    
    var simulate = bound.$bind(bound);
    
    var i = 0;
    
    var noJIT_bound = function () {
      return this.value;
    };
    set(noJIT_bound, function () {
      return ++i;
    });
    
    var noJIT_native = noJIT_bound.bind(noJIT_bound);
    
    var noJIT_lookup = function () {
        return noJIT_bound.value;
    };
    
    var noJIT_simulate = noJIT_bound.$bind(noJIT_bound);
    
    var result;

Test runner

Ready to run.

Testing in
TestOps/sec
native
result = native();
ready
lookup
result = lookup();
ready
simulate
result = simulate();
ready
no JIT native
result = noJIT_native();
ready
no JIT lookup
result = noJIT_lookup();
ready
no JIT simulate
result = noJIT_simulate();
ready

Revisions

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

  • Revision 1: published by WebReflection on