Closure vs. property

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function ClosureColor(name) {
        // Have no choice but to add these methods to the instance
        Object.defineProperty(this, 'name', {
            get: function () {
                return name;
            },
            set: function (n) {
                name = n;
            }
        });
    }
    var closureColor = new ClosureColor('green');
   
   
    function PropertyColor(name) {
        this._name = name;
        Object.defineProperty(this, 'name', {
            get: function () {
                return this._name;
            },
            set: function (n) {
                this._name = n;
            }
        });
    }
    var propertyColor = new PropertyColor('green');
   
   
    function PrototypeColor(name) {
        this._name = name;
    }
    Object.defineProperty(PrototypeColor.prototype, {
        get: function () {
            return this._name;
        },
        set: function (n) {
            this._name = n;
        }
    });
    var prototypeColor = new PrototypeColor('green');
   
};
</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.name;  // get
closureColor.name = 'blue';  // set
 
pending…
Property
var name = propertyColor.name;  // get
propertyColor.name = 'blue';  // set
 
pending…
Prototype
var name = prototypeColor.name;  // get
prototypeColor.name = 'blue';  // set
 
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