Correct way to declare prototype functions

JavaScript performance comparison

Test case created by Hugo Firth

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var Object1 = function() {}
   
    Object1.prototype.defaults = {
        radius: 400,
        up: 1
    }
   
    Object1.prototype.centerOffset = function() {
        return this.defaults.radius*this.defaults.up;
    }
   
    var Object2 = function() {}
   
    Object2.prototype = {
        defaults: {
            radius: 400,
            up: 1
        },
   
        centerOffset: function() {
            return this.defaults.radius*this.defaults.up;
        }
    }
   
};
</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
Define prototype functions separately
var o = new Object1();
var offset = o.centerOffset();
pending…
Define prototype functions in a dictionary
var o = new Object2();
var offset = o.centerOffset();
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