Closure vs. property (v3)

Revision 3 of this benchmark created on


Setup

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');

Test runner

Ready to run.

Testing in
TestOps/sec
Closure
var name = closureColor.name;  // get
closureColor.name = 'blue';  // set
 
ready
Property
var name = propertyColor.name;  // get
propertyColor.name = 'blue';  // set
 
ready
Prototype
var name = prototypeColor.name;  // get
prototypeColor.name = 'blue';  // set
 
ready

Revisions

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