prototype vs closures

JavaScript performance comparison

Revision 56 of this test case created by Peter Lisovin

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  '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');

};
</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
p = new Person('John');
pending…
Closure init
c = person('John');
pending…
Prototype read
p.get_name();
pending…
Closure read
c.get_name();
pending…
Prototype write
p.set_name('Jane');
pending…
Closure write
c.set_name('Jane');
pending…
Prototype doing useful
p.do_something_useful(10000);
pending…
Closure doing useful
c.do_something_useful(10000);
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