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();
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;
--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 |
Test name | Executions per second |
---|---|
Read from data property | 139510256.0 Ops/sec |
Write to data property | 123324936.0 Ops/sec |
Read from accessor method | 125423768.0 Ops/sec |
Write to mutator method | 137977936.0 Ops/sec |
Read from object literal property | 55874512.0 Ops/sec |
Write to object literal property | 12865698.0 Ops/sec |
Read from object literal property on prototype | 137897200.0 Ops/sec |
Write to object literal property on prototype | 98645456.0 Ops/sec |
Let's break down the benchmark test cases and explain what is being tested, the pros and cons of each approach, and other considerations.
Benchmark Definition
The benchmark tests the performance differences between three approaches to access and modify properties in JavaScript:
data.property
).objectLiteralAccessor.property
).method.getProperty()
).Pros and Cons of Each Approach
Other Considerations
prototypeAccessor
test case also accesses the property through the prototype chain.Library and Dependencies
There is no explicit library or dependency mentioned in the benchmark definition or test cases. However, it's worth noting that some JavaScript engines, like V8, have optimized built-in support for accessor properties and getters/setters.
Special JS Features or Syntax
The benchmark does not explicitly use any special JavaScript features or syntax beyond standard language constructs. However, the use of getter and setter functions (getter/setter methods) may be a more advanced concept that not all developers are familiar with.
In summary, this benchmark tests the performance differences between three approaches to access and modify properties in JavaScript: data properties, accessor properties, and getter/setter methods. Each approach has its pros and cons, and understanding these trade-offs is essential for writing efficient, readable, and maintainable code.