Call vs Apply vs Bind (v2)

Revision 2 of this benchmark created by Esailija on


Preparation HTML

<script>
function A() {
    this.sum = 0;
}
A.prototype.fn = function() {
   this.sum++;
   return this;
};
</script>

Setup

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

Teardown


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

Test runner

Ready to run.

Testing in
TestOps/sec
direct
that.fn();
ready
direct (local)
fn();
ready
call
fn.call(that);
ready
apply
fn.apply(that);
ready
bind
fnBind();
ready
method
a.fn();
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