Object or prototype: constructor and methods

JavaScript performance comparison

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);
};
</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
new createObj(2);
pending…
Prototype constructor
new createProtoObj(2);
pending…
Object methods
obj.getData();
obj.setData(5);
obj.getData();
pending…
Prototype methods
proto.getData();
proto.setData(5);
proto.getData();
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