prototype vs closures (v31)

Revision 31 of this benchmark created on


Setup

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');

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype public read
_p.name;
ready
Closure public read
_c.name;
ready
Prototype public write
_p.name = 'John';
ready
Closure public write
_c.name = 'John';
ready
Prototype private get
_p.get_true_name();
ready
Closure private get
_c.get_true_name();
ready
Prototype private set
_p.set_true_name('John');
ready
Prototype init
var p = new Person('John');
ready
Closure init
var c = person('John');
ready
Prototype public get
_p.get_name();
ready
Closure public get
_c.get_name();
ready
Prototype public set
_p.set_name('Jane');
ready
Closure public set
_c.set_name('Jane');
ready
Closure private set
_c.set_true_name('John');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.