var obj = {}
var cnt = 1000
for (let i = 0; i < cnt; i++) {
obj[i] = i + ''
}
var proxyReflect = new Proxy(obj, {
get(obj, prop, reciever) {
return Reflect.get(obj, prop, reciever)
}
})
var proxy = new Proxy(obj, {
get(obj, prop, reciever) {
return obj[prop]
}
})
var protoObj = Object.create(obj, {})
for (let i = 0; i < cnt; i++) {
proxy[i]
}
for (let i = 0; i < cnt; i++) {
protoObj[i]
}
for (let i = 0; i < cnt; i++) {
obj[i]
}
for (let i = 0; i < cnt; i++) {
proxyReflect[i]
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Proxy | |
Prototype | |
Direct | |
Proxy reflect |
Test name | Executions per second |
---|---|
Proxy | 7355.5 Ops/sec |
Prototype | 11314.9 Ops/sec |
Direct | 15590.8 Ops/sec |
Proxy reflect | 4629.5 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark is comparing four different approaches to access and manipulate an object:
obj
object using dot notation (obj[i]
).Object.create()
method to create a new object that inherits from the original obj
object, and then accessing its properties using dot notation (protoObj[i]
).obj
object, and then accessing its properties using dot notation (proxy[i]
).Reflect.get()
method to access properties on the wrapped object.Options Compared
The benchmark is comparing the performance of these four approaches:
Object.create()
to create a new object that inherits from the original object.Reflect.get()
to access properties on the wrapped object.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Reflect.get()
to access properties on the wrapped object. This approach can provide more control over access behavior than traditional Proxies.Library
None of the approaches use any external libraries.
Special JS Feature or Syntax
There is no special JavaScript feature or syntax being tested in this benchmark.
Other Considerations
obj
has a large number of properties (cnt=1000
). This may affect the performance of each approach.Alternative Approaches
Some alternative approaches to testing object access behavior could include:
for...in
loops instead of direct property access.JSON.parse()
instead of string concatenation (i + ''
).get()
trap in a Proxy object.However, these alternative approaches would likely change the focus and scope of the benchmark, which may not be desirable if the goal is to compare the performance of direct property access versus other approaches.