apply vs call vs invoke

JavaScript performance comparison

Revision 8 of this test case created by JulienW

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {
      fn: function(a,b) {return a + b; }
    };
   
    var fn = obj.fn,
        fnJsBound = jsBind(obj, fn),
        fnBound = function() {};
   
    if (fn.bind) {
      fnBound = fn.bind(obj);
    }
   
    function jsBind(self, fn) {
      return function() {
        return fn.apply(self, arguments);
       };
    }
   
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
bind (native) (run if Function.prototype.bind exists)
fnBound(1, 2);
pending…
bind (js)
fnJsBound(1, 2);
pending…
direct invoke
fn(1, 2);
pending…
direct invoke on obj
obj.fn(1, 2);
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