Call vs Apply vs Bind (v3)

Revision 3 of this benchmark created by GusC on


Preparation HTML

<script>
function A() {
    this.sum = 0;
}
A.prototype.fn = function(x,y) {

   this.sum=x+y

   return this;
};
</script>

Setup

var a = new A();
    var sum = 0;
    var fn = function(x, y) {
      sum = x + y;
      return this;
    };
    
    var that = {};
    that.fn = fn;
    
    var fnBind = fn.bind(that, 1);

Teardown


    if (sum < 1 && a.sum < 1) throw new Error()
  

Test runner

Ready to run.

Testing in
TestOps/sec
Object method
that.fn(1, 1);
ready
Local function
fn(1, 1);
ready
Call
fn.call(that, 1, 1);
ready
Apply
fn.apply(that, [1, 1]);
ready
Bind
fnBind(1);
ready
Prototype method
a.fn(1, 1);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Kristina Garfinkel on
  • Revision 2: published by Esailija on
  • Revision 3: published by GusC on