Closure vs. property

JavaScript performance comparison

Revision 5 of this test case created

Preparation code

<script>
function ClosureColor(name) {
    // Have no choice but to add these methods to the instance
    this.getName = function () {
        return name;
    };
    this.setName = function (n) {
        name = n;
    };
}
var closureColor = new ClosureColor('green');


function PropertyColor(name) {
    this._name = name;
    this.getName = function () {
        return this._name;
    };
    this.setName = function (n) {
        this._name = n;
    };
}
var propertyColor = new PropertyColor('green');


function PrototypeColor(name) {
    this._name = name;
}
PrototypeColor.prototype.getName = function () {
    return this._name;
};
PrototypeColor.prototype.setName = function (n) {
    this._name = n;
};
var prototypeColor = new PrototypeColor('green');
</script>
<script>
Benchmark.prototype.setup = function() {
   
   
};
</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
Closure
var name = closureColor.getName();
closureColor.setName('blue');
 
pending…
Property
var name = propertyColor.getName();
propertyColor.setName('blue');
 
pending…
Prototype
var name = prototypeColor.getName();
prototypeColor.setName('blue');
 
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