Call vs Apply vs Direct vs Wrapped

JavaScript performance comparison

Revision 6 of this test case created by bashor

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var update = function(args) {
      this.name = args.name;
      this.age = args.age;
      this.size = args.size;
    };
    var update2 = function(that, args) {
      that.name = args.name;
      that.age = args.age;
      that.size = args.size;
    };
    var update2a = function(args, that) {
      that.name = args.name;
      that.age = args.age;
      that.size = args.size;
    };
    function concat(first, args) {
      var a = [first];
      for (var i = 0; i < args.length; i++)
        a.push(args[i]);
      return a;
    }
    var update2w = function() {
      return update2.apply(undefined, concat(this, arguments));
    }
    var update2ws = function(arg) {
      update2(this, arg)
    }
    var update3 = update.call.bind(update);
    var update3a = update.apply.bind(update);
    var updateW = function(that, args) {
      update.call(that, args)
    }
   
    var updateW1 = function(that, args) {
      return update.apply(that, args);
    }
    var updateW2 = function() {
      arguments[arguments.length++] = this;
      return update2a.apply(this, arguments);
    }
   
    var person = {
      name: 'Marvin',
      age: 42,
      size: 'S',
      update: update
    };
    var newInfo = {
      name: "Andrew",
      age: 23,
      size: 'M'
    };
   
    var L = 100;
};
</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
call
for(var i = 0; i < L; i++)
update.call(person, newInfo);
pending…
apply
for(var i = 0; i < L; i++)
update.apply(person, [newInfo]);
pending…
direct as method
for(var i = 0; i < L; i++)
person.update(newInfo);
pending…
direct as function
for(var i = 0; i < L; i++)
update2(person, newInfo)
pending…
update2 wrapped call (with apply)
for(var i = 0; i < L; i++)
update2w(person, newInfo)
pending…
update2 wrapped to special (with direct call)
for(var i = 0; i < L; i++)
update2ws(person, newInfo)
pending…
update3
for(var i = 0; i < L; i++)
update3(person, newInfo)
pending…
update3a
for(var i = 0; i < L; i++)
update3a(person, [newInfo])
pending…
updateW1
for(var i = 0; i < L; i++)
updateW1(person, [newInfo])
pending…
updateW2
for(var i = 0; i < L; i++)
updateW2(person, newInfo)
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