direct v call v apply

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var A;
    A = (function() {
      function f2(a, b, c) {
          var _a = a.toString();
          var _b = b.toString();
          var _c = c.toString();
          }
         
      var f3 =   function(a, b, c) {
               var _a = a.toString();
          var _b = b.toString();
          var _c = c.toString();
   
          }
   
         
      function A() {}
      A.prototype = {
        fn: function(a, b, c) {
               var _a = a.toString();
          var _b = b.toString();
          var _c = c.toString();
   
        },
        _direct: function(a, b, c) {
          this.fn(a, b, c);
        },
        _cached_f2: function(a, b, c) {
          f2(a, b, c);
        },
        _cached_f3: function(a, b, c) {
          f3(a, b, c);
        },
        _call: function(a, b, c) {
          f2.call(this, a, b, c);
        },
        _apply_array: function(arr) {
          f2.apply(this, arr);
        },
        _apply_arguments: function(a, b, c) {
          f2.apply(this, arguments);
        }
      };
   
      return A;
   
    })();
    var a = 10,
        b = "something",
        c = [10, 20, 30];
    var arr = [a, b, c];
    var inst = new A();
};
</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
Direct
inst._direct(a, b, c);
pending…
Call
inst._call(a, b, c);
pending…
Apply - array
inst._apply_array(arr);
pending…
Apply - arguments
inst._apply_arguments(a, b, c);
pending…
Call cached 1
inst._cached_f2(a,b,c);
pending…
Call cached 2
inst._cached_f3(a,b,c);
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