prototype vs closures

JavaScript performance comparison

Revision 97 of this test case created by JamesAlias

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  'use strict';
  
  function Person1(name) {
    this.name = name;
  }
  
  Person1.prototype.get_name = function () {
    return this.name;
  };
  
  Person1.prototype.set_name = function (name) {
    this.name = name;
  };
  
  
  var createPerson2 = function (name) {
    var p = {
      name: name
    };
  
    return {
      get_name: function () {
        return p.name;
      },
      set_name: function (name) {
        p.name = name;
      }
    };
  };
  
  
  function get_name(p) {
    return p.name;
  }
  
  function set_name(p, name) {
    p.name = name;
  }
  
  var createPerson3 = function (name) {
    var p = {
      name: name
    };
    return {
      get_name: get_name.bind(null, p),
      set_name: set_name.bind(null, p)
    }
  };
  
  var i;
  var p1List = [];
  var p2List = [];
  var p3List = [];
  
  for (i = 0; i < 10000; i += 1) {
    p1List.push(new Person1('John #' + i));
    p2List.push(createPerson2('John2 #' + i));
    p3List.push(createPerson3('John3 #' + i));
  }
  
  var _p1 = new Person1('John');
  var _p2 = createPerson2('Jane');
  var _p3 = createPerson3('Doe');

};
</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 init
var p1 = new Person1('John');
pending…
Closure init
var p2 = createPerson2('John');
pending…
Prototype read
_p1.get_name();
pending…
Closure read
_p2.get_name();
pending…
Prototype write
_p1.set_name('Jane');
pending…
Closure write
_p2.set_name('Jane');
pending…
Closure 2 init
var p3 = createPerson3('John');
pending…
Closure 2 read
_p3.get_name();
pending…
Closure 2 write
_p3.set_name('Jane');
pending…
Prototype multiple read
for (i = 0; i < p1List.length; i += 1) {
  p1List[i].get_name();
}
pending…
Prototype multiple write
for (i = 0; i < p1List.length; i += 1) {
  p1List[i].set_name('Jane #' + i);
}
pending…
Closure multiple read
for (i = 0; i < p2List.length; i += 1) {
  p2List[i].get_name();
}
pending…
Closure multiple write
for (i = 0; i < p2List.length; i += 1) {
  p2List[i].set_name('Jane #' + i);
}
pending…
Closure 2 multiple read
for (i = 0; i < p3List.length; i += 1) {
  p3List[i].get_name();
}
pending…
Closure 2 multiple write
for (i = 0; i < p3List.length; i += 1) {
  p3List[i].set_name('Jane #' + i);
}
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