object = { value: 'data', getData() {return this.value;} };
Object.defineProperty(object, 'dataProperty', {
get() { return this.value; }
});
proxy = new Proxy(object, {})
proxyWithReflectHandler = new Proxy(object, {
get(target, prop, receiver) {
return Reflect.get(target, prop, receiver)
}
})
proxyWithHandler = new Proxy(object, {
get(target, prop, receiver) {
return object[prop];
}
})
object.value;
proxy.value
proxyWithHandler.value
object.getData();
object.dataProperty;
proxyWithReflectHandler.value
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object access | |
Proxy access | |
Proxy with get handler access | |
Use of simple getter | |
Use of defineProperty | |
Proxy with get reflect handler access |
Test name | Executions per second |
---|---|
Object access | 178138112.0 Ops/sec |
Proxy access | 71120072.0 Ops/sec |
Proxy with get handler access | 52001224.0 Ops/sec |
Use of simple getter | 200457440.0 Ops/sec |
Use of defineProperty | 56855296.0 Ops/sec |
Proxy with get reflect handler access | 39178868.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Definition
The provided JSON represents a benchmark that tests two approaches to accessing object properties: using the .
operator (object.value
) and using the proxy
object with a proxy handler (proxy.value
). The benchmark also tests using a simple getter function (object.getData()
), using defineProperty
to create a property on an object, and testing access through a custom proxy handler with both get
handlers (proxyWithReflectHandler
and proxyWithHandler
).
Options Compared
The benchmark compares the following options:
.
operator to access an object property (object.value
).proxy
) with no handler and accessing its properties through it (proxy.value
).object.getData()
).defineProperty
and accessing it directly (object.dataProperty
).Reflect.get
function to resolve property accesses, and accessing its properties through it (proxyWithReflectHandler.value
).proxyWithHandler.value
).Pros and Cons
Here are some pros and cons of each approach:
Reflect
API.Other Considerations
When implementing these approaches, consider the following factors:
Library Use
The benchmark uses the Proxy
API, which is a built-in JavaScript API that provides a way to create proxy objects. The Reflect.get
function is used within the proxy handler to resolve property accesses.
Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax beyond what's described in the benchmark definition.