bind vs closure

Benchmark created by Brad on


Setup

var MyClass = (function () {
      
      function myBind(func, context) {
        return function() {
          func.call(context);
        } 
      }
  
      function MyClass() {
          var _this = this;
          var e = 1;
          var d = 2;
          var c = 3;
          var b = 4;
          this.a = 5;
          this.closure = function () {
              var b = _this.a * 2;
              return b;
          };
          this.bound = this.bound.bind(this);
          this.myBoundFunction = myBind(this.myBoundFunction, this);
      }
      MyClass.prototype.callback = function (cb) {
          cb();
      };
      MyClass.prototype.bound = function () {
          var b = this.a * 2;
          return b;
      };
  
      MyClass.prototype.myBoundFunction = function() {
          var b = this.a * 2;
          return b;
      }
  
      return MyClass;
  }());
  
  var test = new MyClass();

Test runner

Ready to run.

Testing in
TestOps/sec
Custom Bound Tets
test.callback(test.myBoundFunction);
ready
Bind Test
test.callback(test.bound);
ready
Closure Test
test.callback(test.closure);
ready

Revisions

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