prototype vs closures

JavaScript performance comparison

Revision 82 of this test case created by ys

Preparation code

<script>
  function Person(name) {
      this.name = name;
  }
  
  Person.prototype.get_name = function() {
      return this.name;
  };
  
  Person.prototype.set_name = function(name) {
      this.name = name;
  };
  
  get_name = function(self) {
return function() {
return self.name;}
}

  set_name = function(self) {
return function(val) {
self.name = val;}
}

  var person = function(name) {
          var self = {
           name: name,
           get_name: get_name(self),
           set_name: set_name(self)
          };
          return self;
      };
  
  var c = new Person('John');
  var b = person('John');
  
  ui.benchmarks[2].setup = function() {
      var b = new Person('John');
  };
  
  ui.benchmarks[3].setup = function() {
      var c = person('John');
  };
  
  ui.benchmarks[4].setup = ui.benchmarks[2].setup;
  ui.benchmarks[5].setup = ui.benchmarks[3].setup;
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Prototype init
var c = new Person('John');
pending…
Closure init
var b = person('John');
pending…
Prototype read
c.get_name();
pending…
Closure read
b.get_name();
pending…
Prototype write
c.set_name('Jane');
pending…
Closure write
b.set_name('Jane');
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.

0 Comments