bind vs closure
JavaScript performance comparison
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 >= this.minVal) ? 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.
| Test | Ops/sec | |
|---|---|---|
closure method |
|
pending… |
bind method |
|
pending… |
proxy method |
|
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:
- Revision 1: published
- Revision 2: published
- Revision 3: published by concat-multiple-vals
- Revision 4: published
- Revision 5: published
- Revision 6: published
0 comments