Data Properties vs. Accessor Properties vs. Getter / Setter Methods

JavaScript performance comparison

Revision 4 of this test case created by Jürg Lehni

Info

The purpose of this benchmark is to determine the performance differences between data properties, accessor properties and getters / setter functions.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var data = {
        property: property
    };
   
    var property = undefined;
   
    var accessor = {
        get property() {
            return property;
        },
        set property(value) {
            property = value;
        }
    };
    var method = {
        getProperty: function() {
            return property;
        },
   
        setProperty: function(value) {
            property = value;
        }
    };
};
</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
Write to data property
data.property = true;
pending…
Write to accessor property
accessor.property = true;
pending…
Write to setter method
method.setProperty(true);
pending…
Read from data property
var value = data.property;
pending…
Read from accessor property
var value = accessor.property;
pending…
Read from getter method
var value = method.getProperty();
 
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