Object or prototype: constructor and methods

JavaScript performance comparison

Revision 2 of this test case created by Pencroff

Info

Compare using prototype methods and exemplar methods: executing constructor and executing methods

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    createObj = function (v) {
    this.data = v;
    this.getData = function() {return this.data};
    this.setData = function(v) {this.data = v};
    };
    obj = new createObj(2);
    createProtoObj = function (v) {
    this.data = v;
    };
    createProtoObj.prototype.getData = function() {return this.data};
    createProtoObj.prototype.setData = function(v) {this.data = v};
    proto = new createProtoObj(2);
    literal = {
      data: 2,
      getData: function() {return this.data},
      setData: function(v) {this.data = v}
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Object constructor
var o = new createObj(2);
pending…
Prototype constructor
var o = new createProtoObj(2);
pending…
Object methods
obj.getData();
obj.setData(5);
obj.getData();
pending…
Prototype methods
proto.getData();
proto.setData(5);
proto.getData();
pending…
Literal object
var o = {
  data: 2,
  getData: function() {return this.data},
  setData: function(v) {this.data = v}
}
pending…
Literal methods
literal.getData();
literal.setData(5);
literal.getData();
pending…
Empty literal
var o = {};
pending…
Empty constructor
var o = new function(){};
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. Here’s a list of current revisions for this page:

0 comments

Add a comment