object = { value: 'data' };
function getValue() {
return object.value
}
proxyWithHandler = new Proxy(object, {
get(target, prop, receiver) {
return target[prop]
}
})
object.value;
proxyWithHandler.value
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object access | |
Proxy with get handler access |
Test name | Executions per second |
---|---|
Object access | 12798525.0 Ops/sec |
Proxy with get handler access | 10329571.0 Ops/sec |
Let's break down the provided benchmark JSON and test cases.
Benchmark Definition
The benchmark definition is a script that creates an object object
with a property value
containing the string 'data'
. It then defines a function getValue()
that returns the value of object.value
. Additionally, it creates a proxy instance proxyWithHandler
with a get handler that simply returns the value of the requested property. The script preparation code is the following:
object = { value: 'data' };
function getValue() {
return object.value;
}
proxyWithHandler = new Proxy(object, {
get(target, prop, receiver) {
return target[prop];
}
});
Options Compared
The benchmark compares two options:
object.value
syntax is used to access the value of the object
.proxyWithHandler.value
syntax is used to access the value of the proxyWithHandler
instance.Pros and Cons
Direct Object Access (Object)
Pros:
Cons:
Proxy with Get Handler
Pros:
Cons:
Other Considerations
Library Used (Proxy)
The Proxy
constructor is a built-in JavaScript function that creates a proxy instance. A proxy is an object that provides additional functionality or behavior when properties are accessed, such as validation, encryption, or transformation.
In this benchmark, the proxy is used to create a handler for getting values from the object
. The handler simply returns the value of the requested property without modifying it.
Special JS Feature/ Syntax
There is no special JavaScript feature or syntax used in this benchmark. It only relies on standard JavaScript features and constructs.
Alternatives
If you want to optimize object access, you can consider using other techniques such as:
const
or let
instead of var
for variable declarations.However, in this specific benchmark, the focus is on comparing direct object access with proxy-based access, so it's essential to carefully consider the trade-offs between simplicity, performance, and complexity.