call vs apply

JavaScript performance comparison

Revision 34 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Obj() {
          var that = "that";
    }
    Obj.prototype.target = function(test) {
       return test;
    };
    var yes_array = ["YES"];
    var yes_string = "YES";
   
    var scope = {};
    scope._tmp = Obj.prototype.target;
   
    var that = Obj.prototype;
    var wrapped = function(){
    that.target(yes_string);
    };
};
</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
apply
Obj.prototype.target.apply(scope, ["YES"]);  
pending…
call
Obj.prototype.target.call(scope, "YES");
pending…
apply, existing array
Obj.prototype.target.apply(scope, yes_array);
pending…
call, existing string
Obj.prototype.target.call(scope, yes_string);
pending…
direct tmp assign - cached
scope._tmp(yes_string);
pending…
direct tmp assign
scope.__tmp = Obj.prototype.target;
scope.__tmp(yes_string);
delete scope.__tmp
pending…
direct call - just to compare (incorrect)
Obj.prototype.target(yes_string);
pending…
wrapped
wrapped();
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