Closure vs. property

Benchmark created by Axel Rauschmayer on


Setup

function ClosureColor(name) {
        Object.defineProperties(this, {
            // Have no choice but to add these methods to the instance
            name: {
                get: function () {
                    return name;
                },
                set: function (n) {
                    name = n;
                }
            }
        });
    }
    function PropertyColor(name) {
        this._name = name;
        Object.defineProperties(this, {
            name: {
                get: function () {
                    return this._name;
                },
                set: function (n) {
                    this._name = n;
                }
            }
        });
    }
    
    function PrototypeColor(name) {
        this._name = name;
    }
    PrototypeColor.prototype = {
        get name() {
            return this._name;
        },
        set name(name) {
            this._name = name;
        }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Closure
var c = new ClosureColor('green');
var name = c.name;
c.name = 'blue';
ready
Property
var c = new PropertyColor('green');
var name = c.name;
c.name = 'blue';
ready
Prototype
var c = new PrototypeColor('green');
var name = c.name;
c.name = 'blue';
ready

Revisions

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