<b>Comparing performance of:</b> Read from data property vs Write to data property vs Read from accessor method vs Write to mutator method vs Read from object literal property vs Write to object literal property vs Read from object literal property on prototype vs Write to object literal property on prototype
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;
},
getProperty() {
return property;
},
setProperty(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 | |
Read with prototype method | |
Write with prototype method |
Test name | Executions per second |
---|---|
Read from data property | 2388566528.0 Ops/sec |
Write to data property | 1466171776.0 Ops/sec |
Read from accessor method | 649753152.0 Ops/sec |
Write to mutator method | 1341940864.0 Ops/sec |
Read from object literal property | 649740544.0 Ops/sec |
Write to object literal property | 1275260160.0 Ops/sec |
Read from object literal property on prototype | 677672448.0 Ops/sec |
Write to object literal property on prototype | 1364491520.0 Ops/sec |
Read with prototype method | 848672320.0 Ops/sec |
Write with prototype method | 1359838464.0 Ops/sec |
Benchmark Overview
The MeasureThat.net benchmark measures the performance differences between three approaches to accessing and modifying data in JavaScript:
data.property
) to access and modify properties directly on an object.objectLiteralAccessor.property
).method.getProperty()
).Options Compared
The benchmark compares the performance of each approach:
data.property
)objectLiteralAccessor.property
, accessorProperty
)method.getProperty()
, method.set()
) on a prototype objectPerformance Comparison
The benchmark results show the execution speed of each approach, measured in executions per second (FPS). The fastest approach is generally used as the default or recommended method.
Key Observations
Reasons for Performance Differences
The performance differences between these approaches can be attributed to various factors:
Best Practices
Based on these results, the recommended approach is to use data properties when possible, as they offer the best balance between readability and performance. However, if using accessor properties or getter/setter methods is necessary for specific requirements (e.g., encapsulation, validation), it's essential to consider the potential performance trade-offs and optimize accordingly.
Please note that these observations and recommendations are based on a specific JavaScript engine and environment. Results may vary depending on other factors, such as version, optimization levels, or specific use cases.