bind vs closure

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var obj = { 
   minVal : 20,
   items : [10,20,50,1,30,40,50,500,200,5],
  			
  checkIfValid: function(a){ 
  	return (a >= this.minVal) ? true : false; 
  },
  			
  closureMechanism: function() {
  	var that = this,
  	    numValids = this.items.map(function(item) {
  		return that.checkIfValid(item);
  	    });
          return numValids;	
  },
  closureWay: function(){
    var numValids;
    (function(self) {
  	    numValids = self.items.map(self.checkIfValid);
    })(this);
  
    return numValids;
  },
  bindMechanism: function(){
  	var numValids = this.items.map(this.checkIfValid.bind(this));
  	    return numValids;
  },
  proxyMechanism: function(){
     var numValids = this.items.map($.proxy(this.checkIfValid,this));
     return  numValids;
  }
  
  };

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
closure method
obj.closureMechanism();

pending…
bind method
obj.bindMechanism();
pending…
proxy method
obj.proxyMechanism();
pending…
closure way
obj.closureWay();
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.

0 Comments