prototype vs closures

JavaScript performance comparison

Revision 51 of this test case created by

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;
  };
  
  var person = (function() {

"use strict";

            function Person(name) {
      this.name = name;
  }
  
  Person.prototype.get_name = function() {
      return this.name;
  };
  
  Person.prototype.set_name = function(name) {
      this.name = name;
  };

return Person;
      }());
  
  var c = new Person('John');
  var b = new person('John');
  
  ui.benchmarks[2].setup = function() {
      var b = new Person('John');
  };
  
  ui.benchmarks[3].setup = function() {
      var c = new 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 read
c.get_name();
pending…
Closure init
var b = new person('John');
pending…
Prototype init
var c = new Person('John');
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.

1 Comment

Mo commented :

The code for this version is not valid:

All you are doing is defining one prototype globally and the other prototype in a closure. But you are still comparing prototype to prototype, not prototype to closure as promised by the title (for real results look at previous versions).