bind vs closure

JavaScript performance comparison

Revision 2 of this test case created

Info

to compare if using closure

Preparation code

<script>
  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() {
      return this.items.map(this.closured);
    },

    bindMechanism: function() {
      return this.items.map(this.bound);
    }
  };

obj.bound = obj.checkIfValid.bind(obj);
obj.closured = (function() {
  var that = obj;
  return function(item) {
    return that.checkIfValid(item);
  };
})();
</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…

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