Closure vs. property

JavaScript performance comparison

Revision 19 of this test case created by mucaho

Preparation code

<script>
function ClosureColor(name) {
    return {
        getName: function() {
            return name;
        },
        setName: function(newName) {
            name = newName;
        }
    };
}
var closureColor = 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() {
  var closureColor = ClosureColor('green');
  var propertyColor = new PropertyColor('green');
  var prototypeColor = new PrototypeColor('green');
  var str1 = "red";
  var str2 = "blue";
  var a = false;

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Closure
closureColor.setName(str1);
a = (closureColor.getName() === str1);

closureColor.setName(str2);
a = (closureColor.getName() === str2);
pending…
Property
propertyColor.setName(str1);
a = (propertyColor.getName() === str1);

propertyColor.setName(str2);
a = (propertyColor.getName() === str2);
pending…
Prototype
prototypeColor.setName(str1);
a = (propertyColor.getName() === str1);

prototypeColor.setName(str2);
a = (propertyColor.getName() === str2);
pending…
Closure#2
closureColor.setName(str1);
a = (closureColor.getName() === str1);

closureColor.setName(str2);
a = (closureColor.getName() === str2);
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.

0 Comments