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

JavaScript performance comparison

Revision 5 of this test case created by Jürg Lehni and last updated

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 method = {
        getProperty: function() {
            return property;
        },
   
        setProperty: function(value) {
            property = value;
        }
    };
   
    var objectLiteralAccessor = {
        get property() {
            return property;
        },
        set property(value) {
            property = value;
        }
    };
   
    var definePropertyAccessor = {};
    Object.defineProperty(definePropertyAccessor, 'property', {
        get: function() {
            return property;
        },
        set: function(value) {
            property = value;
        }
    });
   
    function Class() {
    }
   
    Class.prototype = {
        get property() {
            return property;
        },
        set property(value) {
            property = value;
        }
    };
   
    var prototypeAccessor = new Class();
};
</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
Read from data property
var value = data.property;
pending…
Write to data property
data.property = true;
pending…
Read from getter method
var value = method.getProperty();
 
pending…
Write to setter method
method.setProperty(true);
pending…
Read from object literal accessor property
var value = objectLiteralAccessor.property;
pending…
Write to object literal accessor property
objectLiteralAccessor.property = true;
pending…
Read from Object.defineProperty() accessor property
var value = definePropertyAccessor.property;
pending…
Write to Object.defineProperty() accessor property
definePropertyAccessor.property = true;
pending…
Read from object literal accessor property on prototype.
var value = prototypeAccessor.property;
pending…
Write to object literal accessor property on prototype.
prototypeAccessor.property = true;
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