prototype vs closures

JavaScript performance comparison

Revision 44 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var Person = function(name, lastname, age) {
    this.name = name;
    this.lastname = lastname;
    this.age = age;
  }
  
  Person.prototype.get_name = function() {
    return this.name;
  };
  
  Person.prototype.toString = function() {
    return this.name + ' ' + this.lastname + ' (' + this.age + ')';
  };
  
  
  
  var person = function(name, lastname, age) {
    var name = name;
    var lastname = lastname;
    var age = age;
  
    function get_name() {
      return name;
    };
  
    function toString() {
      return name + ' ' + lastname + ' (' + age + ')';
    };
    return {
      get_name: get_name,
      toString: toString
    };
  };
  
  _p = [];
  _c = [];
  var i;
  for (i = 0; i < 100; ++i) {
    _p.push(new Person('John', 'Doe', 42));
    _c.push(person('John', 'Doe', 42));
  }

};
</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
var i, p = _p,
  r;
i = p.length;
while (i-- > 0) {
  r = p[i].toString();
}
pending…
Closure read
var i, c = _c;
i = c.length;
while (i-- > 0) {
  r = c[i].toString();
}
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