Native bind vs lodash vs call vs apply vs closure scope

JavaScript performance comparison

Revision 34 of this test case created by David Bonnet

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.8.1/lodash.min.js">
</script>
<script>
  var lodash = _.noConflict();
</script>
<script>
Benchmark.prototype.setup = function() {
    var bindCall = function(f, scope){
        return function(){
            return f.call(scope, arguments[0]);
        }
    };
    var bindApply = function(f, scope){
        return function(){
            return f.apply(scope, arguments);
        }
    };
    var bindScope = function(f, scope){
        return function(){
            return f(scope, arguments[0]);
        }
    };
    var t1 = (function(x) {
      return this + x;
    }).bind(1);
   
    var t2 = lodash.bind(function(x) {
      return this + x;
    }, 1);
   
    var t3 = bindCall(function(x) {
        return this + x;
    }, 1);
   
    var t4 = bindApply(function(x) {
        return this + x;
    }, 1);
   
    var t5 = bindScope(function(scope, x) {
        return scope + x;
    }, 1);
   
    var n = {
        value: 1,
        sum: function(x) {
            return this.value + x;
        }
    }
   
};
</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
Native bind
t1(1);
pending…
loDash bind
t2(1)
pending…
Custom Bind With Call
t3(1)
pending…
Custom Bind With Apply
t4(1)
pending…
Custom Bind With Scope
t5(1)
pending…
Method Call
n.sum(1)
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