prototype vs closures

JavaScript performance comparison

Revision 102 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function pPerson(name) {
      this.name = name;
  }
  
  pPerson.prototype.get_name = function() { return this.name; };
  pPerson.prototype.set_name = function(name) { this.name = name; };
  
  function cPerson(name) {
      var name_ = name;
      return {
          'get_name': function() { return name_; },
          'set_name': function(name) { name_ = name; }
      };
  };
  
  function oPerson(name) {
      return {
          'name': name,
          'get_name': pPerson.prototype.get_name,
          'set_name': pPerson.prototype.set_name
      };
  };
  
  var _p = [];
  var _c = [];
  var _o = [];
  for (var i = 0; i < 10000; ++i) {
      _p.push(new pPerson('John'));
      _c.push(cPerson('John'));
      _o.push(oPerson('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 read
for (var i = 0; i < 10000; ++i) {
    _p[i].get_name();
}
pending…
Closure read
for (var i = 0; i < 10000; ++i) {
    _c[i].get_name();
}
pending…
Object read
for (var i = 0; i < 10000; ++i) {
    _o[i].get_name();
}
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