prototype vs closures

JavaScript performance comparison

Revision 31 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function Person(name) {
  	this.name = name;
  
    var trueName = 'Rumpelstiltskin';
    this.get_true_name = function() {
      return trueName;
    };
    this.set_true_name = function(name) {
      trueName = name;
    };
  }
  Person.prototype.get_name = function() {
  	return this.name;
  };
  Person.prototype.set_name = function(name) {
  	this.name = name;
  };
  
  var person = function(name) {
    var trueName = 'Rumpelstiltskin';
  
  	return {
      'name': name,
  		'get_name': function() {
  			return this.name;
  		},
  		'set_name': function(name) {
  			this.name = name;
  		},
      'get_true_name': function() {
        return trueName;
      },
      'set_true_name': function(name) {
        trueName = name;
      }
  	};
  };
  
  var _p = new Person('John');
  var _c = person('John');

};
</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 public read
_p.name;
pending…
Closure public read
_c.name;
pending…
Prototype public write
_p.name = 'John';
pending…
Closure public write
_c.name = 'John';
pending…
Prototype private get
_p.get_true_name();
pending…
Closure private get
_c.get_true_name();
pending…
Prototype private set
_p.set_true_name('John');
pending…
Prototype init
var p = new Person('John');
pending…
Closure init
var c = person('John');
pending…
Prototype public get
_p.get_name();
pending…
Closure public get
_c.get_name();
pending…
Prototype public set
_p.set_name('Jane');
pending…
Closure public set
_c.set_name('Jane');
pending…
Closure private set
_c.set_true_name('John');
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