bind vs closure

JavaScript performance comparison

Revision 4 of this test case created

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) {
            var numValids;
            for (i = 0; i < 1000; i++) {
              numValids = that.checkIfValid(item);
            }
            return that.checkIfValid(item);
          });
        return numValids;
      },
      bindMechanism: function() {
        var numValids;
        for (i = 0; i < 1000; i++) {
          numValids = this.items.map(this.checkIfValid.bind(this));
        }
        return numValids;
      },
      proxyMechanism: function() {
        var numValids = this.items.map($.proxy(this.checkIfValid, this));
        return numValids;
      }
   
    };
};
</script>

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