Native bind vs lodash vs call vs apply vs closure scope

JavaScript performance comparison

Revision 58 of this test case created

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(){
            f.call(scope, arguments[0]);
        }
    };
    var bindApply = function(f, scope){
        return function(){
            f.apply(scope, arguments);
        }
    };
    var bindScope = function(f, scope){
        return function(){
            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);
};
</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…

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