Internal definition of class constants vs passing params

JavaScript performance comparison

Test case created by Hugo Firth

Info

A test to determine whether, if some data is going to be standard across all objects of a type/prototype/class, it is more perfomant to:

a: Define the data in the object constructor function.
b: Define the data as a prototype field.
c: Define the data outside the scope of the object function and pass as
   a parameter to the constructor function.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var defaults = {
        radius:400,
        up:1
    }
   
};
</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
Using object constructor function
var Object = function() {
    this.defaults = {
        radius: 400,
        up: 1    
    }
};

var o = new Object();
var someRadius = o.defaults.radius;
pending…
Using a prototype field
var Object = function() {};

Object.prototype.defaults = {
    radius:400,
    up:1
}

var o = new Object();
var someRadius = o.defaults.radius;
 
pending…
Pass a parameter
var Object = function(defaults) {
    this.defaults = defaults;
}

var o = new Object(defaults);
var someRadius = o.defaults.radius;
pending…
Using a prototype function
var Object = function() {
    this.defaults = this._defaults();
}

Object.prototype._defaults = function(){
    return defaults;
}

var o = new Object();
var someRadius = o.defaults.radius;
pending…

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

Compare results of other browsers

0 comments

Add a comment