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 = {
getProperty() {
return property;
},
setProperty(value) {
property = value;
},
get property() {
return property;
},
set property(value) {
property = value;
}
};
var prototypeAccessor = new Class();
var value = data.property;
data.property = true;
var value = method.getProperty();
method.setProperty(true);
var value = objectLiteralAccessor.property;
objectLiteralAccessor.property = true;
var value = prototypeAccessor.property;
prototypeAccessor.property = true;
var value = prototypeAccessor.getProperty();
prototypeAccessor.setProperty(true);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Read from data property | |
Write to data property | |
Read from accessor method | |
Write to mutator method | |
Read from object literal property | |
Write to object literal property | |
Read from object literal property on prototype | |
Write to object literal property on prototype | |
Write to object | |
Read to object |
Test name | Executions per second |
---|---|
Read from data property | 11944928.0 Ops/sec |
Write to data property | 11895602.0 Ops/sec |
Read from accessor method | 5966777.0 Ops/sec |
Write to mutator method | 4444305.5 Ops/sec |
Read from object literal property | 5369902.0 Ops/sec |
Write to object literal property | 2665076.2 Ops/sec |
Read from object literal property on prototype | 5375312.0 Ops/sec |
Write to object literal property on prototype | 4341736.0 Ops/sec |
Write to object | 5721419.5 Ops/sec |
Read to object | 4266714.5 Ops/sec |
Let's break down what is being tested in this benchmark and the different approaches used.
Benchmark Purpose: The purpose of this benchmark is to compare the performance differences between three approaches to access and modify data properties:
data.property
).objectLiteralAccessor.property
) or setter functions to modify the value (e.g., objectLiteralAccessor.property = true;
).method.getProperty()
).Approach Comparison:
Performance Results: The benchmark results show that accessor properties outperform data properties due to their ability to cache the property value. This caching reduces the number of times the property is looked up, leading to faster execution. Getter/setter methods are slower than accessor properties because they involve function calls, which add overhead.
Key Insights:
Overall, this benchmark highlights the importance of choosing an appropriate approach for accessing and modifying data in objects based on performance and security requirements.