prototype vs closures (v56)

Revision 56 of this benchmark created by Peter Lisovin on


Setup

'use strict';
  
  function get_name() {
    return this.name;
  }
  
  function set_name(name) {
    this.name = name;
  }
  
  function do_something_useful(n) {
    var i,
        sum = 0;
    for (i = 0; i < n; i += 1) {
      sum += i;
    }
    return sum;
  }
  
  function Person(name) {
    this.name = name;
  }
  Person.prototype.get_name = get_name;
  Person.prototype.set_name = set_name;
  Person.prototype.do_something_useful = do_something_useful;
  
  var person = function(name) {
    return {
      name: name,
      get_name: get_name,
      set_name: set_name,
      do_something_useful: do_something_useful
    };
  };
  
  var p = new Person('John');
  var c = person('John');

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype init
p = new Person('John');
ready
Closure init
c = person('John');
ready
Prototype read
p.get_name();
ready
Closure read
c.get_name();
ready
Prototype write
p.set_name('Jane');
ready
Closure write
c.set_name('Jane');
ready
Prototype doing useful
p.do_something_useful(10000);
ready
Closure doing useful
c.do_something_useful(10000);
ready

Revisions

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