Data Properties vs. Accessor Properties vs. Getter / Setter Methods (v2)

Revision 2 of this benchmark created by Jürg Lehni on


Description

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

Setup

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;
        }
    };
    
    function Class() {
    }
    
    Class.prototype = {
        get property() {
            return property;
        },
        set property(value) {
            property = value;
        }
    };
    
    var prototypeAccessor = new Class();

Test runner

Ready to run.

Testing in
TestOps/sec
Read from data property
var value = data.property;
ready
Write to data property
data.property = true;
ready
Read from getter method
var value = method.getProperty();
 
ready
Write to setter method
method.setProperty(true);
ready
Read from object literal accessor property
var value = objectLiteralAccessor.property;
ready
Write to object literal accessor property
objectLiteralAccessor.property = true;
ready
Read from object literal accessor property on prototype.
var value = prototypeAccessor.property;
ready
Write to object literal accessor property on prototype.
prototypeAccessor.property = true;
ready

Revisions

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

  • Revision 2: published by Jürg Lehni on