prototype vs closures (v97)

Revision 97 of this benchmark created by JamesAlias on


Setup

'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');

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype init
var p1 = new Person1('John');
ready
Closure init
var p2 = createPerson2('John');
ready
Prototype read
_p1.get_name();
ready
Closure read
_p2.get_name();
ready
Prototype write
_p1.set_name('Jane');
ready
Closure write
_p2.set_name('Jane');
ready
Closure 2 init
var p3 = createPerson3('John');
ready
Closure 2 read
_p3.get_name();
ready
Closure 2 write
_p3.set_name('Jane');
ready
Prototype multiple read
for (i = 0; i < p1List.length; i += 1) {
  p1List[i].get_name();
}
ready
Prototype multiple write
for (i = 0; i < p1List.length; i += 1) {
  p1List[i].set_name('Jane #' + i);
}
ready
Closure multiple read
for (i = 0; i < p2List.length; i += 1) {
  p2List[i].get_name();
}
ready
Closure multiple write
for (i = 0; i < p2List.length; i += 1) {
  p2List[i].set_name('Jane #' + i);
}
ready
Closure 2 multiple read
for (i = 0; i < p3List.length; i += 1) {
  p3List[i].get_name();
}
ready
Closure 2 multiple write
for (i = 0; i < p3List.length; i += 1) {
  p3List[i].set_name('Jane #' + i);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.