object = { value: 'data' };
proxy = new Proxy(object, {})
proxyWithHandler = new Proxy(object, {
get(target, prop, receiver) {
return target[prop]
}
})
object.value;
proxy.value
proxyWithHandler.value
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object access | |
Proxy access | |
Proxy with get handler access |
Test name | Executions per second |
---|---|
Object access | 5426859.5 Ops/sec |
Proxy access | 4304041.5 Ops/sec |
Proxy with get handler access | 4328838.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided JSON represents a benchmark that tests the performance difference between accessing an object directly and using a Proxy object to access its properties.
In the "Script Preparation Code", we have:
object
with a property value
.proxy
created from object
, which means it delegates all accesses to object
. This is done by passing an empty object {}
as the second argument to the Proxy
constructor, which specifies the handler for all properties. The get
method in the handler simply returns the value of the corresponding property on the target object (target
).proxyWithHandler
created from object
, but with a custom get
handler that returns the value of the corresponding property directly, without delegating to target
.The test cases are designed to measure the performance difference between accessing these three scenarios:
object.value;
)proxy.value;
)proxyWithHandler.value;
)Options compared
We have two options being compared:
The key differences between these approaches are:
get
handler, you can manipulate the property access in more complex ways.Pros and Cons
Here's a summary of the pros and cons for each approach:
Library/Utility
In this benchmark, the Proxy
object is used as a utility to create Proxy objects with custom handlers. The get
method is used to specify how properties are accessed on the proxy object.
Special JS feature/syntax
There's no special JavaScript feature or syntax being tested here. The focus is on understanding the performance difference between direct object access and using a Proxy object to access its properties.
Other alternatives
If you're looking for alternative approaches, consider:
Keep in mind that the choice of approach depends on the specific use case and requirements.