Closure vs. property

JavaScript performance comparison

Revision 2 of this test case created by Andrea Giammarchi

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    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;
    }
    Object.defineProperties(PrototypeColor.prototype, {
            name: {
                get: function () {
                    return this._name;
                },
                set: function (n) {
                    this._name = n;
                }
            }
        });
};
</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 c = new ClosureColor('green');
var name = c.name;
c.name = 'blue';
pending…
Property
var c = new PropertyColor('green');
var name = c.name;
c.name = 'blue';
pending…
Prototype
var c = new PrototypeColor('green');
var name = c.name;
c.name = '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