prototype vs closures

JavaScript performance comparison

Revision 68 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function ProtoPerson(name) {
  	this.name = name;
  }
  
  ProtoPerson.prototype.get_name = function() {
  	return this.name;
  };
  
  ProtoPerson.prototype.set_name = function(name) {
  	this.name = name;
  };
  
  var ObjPerson = function(pname) {
  	return {
  		'get_name': function() {
   			return pname;
   		},
  		'set_name': function(name) {
  		 	pname = name;
  		}
  	};
  };
  
  var ClosPerson = function(name) {
    this.set_name = function(n) {
      name=n;
    };
    this.get_name = function() {
      return name;
    };
  }
  
  var _p = new ProtoPerson('John');
  var _o = ObjPerson('John');
  var _c = new ClosPerson('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 get
_p.get_name();
pending…
Closure get
_c.get_name();
pending…
Prototype set
_p.set_name('Jane');
pending…
Closure set
_c.set_name('Jane');
pending…
Plain Object get
_o.get_name();
pending…
Plain Object set
_o.set_name('Jane');
pending…
new Prototype
var dummyp = new ProtoPerson('James');
pending…
new Closure
var dummyc = new ClosPerson('James');
pending…
new plain Object
var dummyo = ObjPerson('James');
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