object = { value: 'data' };
proxy = new Proxy(object, {})
proxyWithHandler = new Proxy(object, {
get(target, prop, receiver) {
return Reflect.get(target, prop, receiver)
}
})
objWithGetter = { _val: 'data', get value() { return this._val } }
object.value;
proxy.value
proxyWithHandler.value
objWithGetter.value
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object access | |
Proxy access | |
Proxy with get handler access | |
Object with getter |
Test name | Executions per second |
---|---|
Object access | 177267792.0 Ops/sec |
Proxy access | 68648184.0 Ops/sec |
Proxy with get handler access | 39520408.0 Ops/sec |
Object with getter | 64418728.0 Ops/sec |
Let's break down the provided JSON benchmark definition and explain what is being tested.
Benchmark Definition
The benchmark measures the performance of accessing properties on different types of objects in JavaScript: regular objects, proxies, and objects with getters.
Script Preparation Code
The script prepares three types of objects:
object
: a regular object with a single property value
containing the string 'data'
.proxy
: a proxy object created from the object
using the Proxy
constructor. The proxy has no handlers defined, meaning it delegates all operations to the underlying object.proxyWithHandler
: another proxy object created from the object
, but this time with a getter handler defined for the value
property. This means that when accessing value
on the proxy object, it will use the get
method of the getter handler instead of delegating to the underlying object.Html Preparation Code
There is no HTML preparation code provided, which suggests that the benchmark is focused solely on JavaScript performance and does not involve DOM-related operations.
Test Cases
The individual test cases are designed to measure the performance of accessing properties on each type of object:
object.value;
- accesses the value
property on a regular object.proxy.value;
- accesses the value
property on a proxy object without a getter handler.proxyWithHandler.value;
- accesses the value
property on a proxy object with a getter handler.objWithGetter.value;
- accesses the value
property on an object with a defined getter function.Library
None of the test cases use any external libraries.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark, other than the use of the Proxy
constructor and getter handlers. This suggests that the benchmark is designed to be language-agnostic and does not rely on specific features or implementations.
Now, let's discuss the pros and cons of each approach:
object.value;
)proxy.value;
)proxyWithHandler.value;
)Other alternatives that could be considered in a similar benchmark include:
Object.defineProperty
or other methods to create getters and setters on regular objects.Keep in mind that the specific alternatives will depend on the goals and scope of the benchmark.