bind vs closure

JavaScript performance comparison

Revision 3 of this test case created by concat-multiple-vals

Info

to compare if using closure

Preparation code

<script src="//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 >= 20) ? true : false;
    },
                       
    closureMechanism: function() {
        var that = this,
            numValids = this.items.map(function(item) {
                return that.checkIfValid(item);
            });
            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 unknown unknown
Test Ops/sec
closure method
obj.closureMechanism();

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