const foo = 5
var data = {
property: foo
};
var method = {
getProperty: function() {
return foo;
},
setProperty: function(value) {
property = value;
}
};
var objectLiteralAccessor = {
get property() {
return foo;
},
set property(value) {
property = value;
}
};
function Class() {
}
Class.prototype = {
get property() {
return foo;
},
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 | 26632470.0 Ops/sec |
Write to data property | 26598838.0 Ops/sec |
Read from accessor method | 26519682.0 Ops/sec |
Write to mutator method | 8889552.0 Ops/sec |
Read from object literal property | 21108186.0 Ops/sec |
Write to object literal property | 6191070.5 Ops/sec |
Read from object literal property on prototype | 27012620.0 Ops/sec |
Write to object literal property on prototype | 8876211.0 Ops/sec |
Let's break down the JavaScript microbenchmark on MeasureThat.net.
Benchmark Overview
The benchmark is designed to compare the performance differences between three approaches:
These approaches are used to access or modify a value within an object.
Options Compared
obj.property = value;
).objectLiteralAccessor.getProperty()
and objectLiteralAccessor.setProperty(value);
).method.getProperty()
and method.setProperty(true);
).Pros and Cons of Each Approach
get
and set
keywords), and can be less efficient due to function call overhead.Library Used (if applicable)
There is no specific library mentioned in the benchmark definition or test cases. However, the Class
constructor with a prototype property is used to demonstrate accessor properties on prototype objects.
Special JS Features or Syntax
This benchmark does not explicitly use any special JavaScript features or syntax, such as async/await, arrow functions, or modernized ECMAScript features.
Other Alternatives
If you'd like to explore alternative approaches, here are some additional options:
Keep in mind that these alternatives might not be as widely supported or optimized for performance compared to the original three approaches.