window.basic = {a : 1, b: 2};
window.proxy = new Proxy({a : 1, b: 2}, {get : (t, p) => t[p]})
window.proto = Object.setPrototypeOf({}, {a : 1, b: 2})
let a = basic.a;
let b = basic.b;
let a = proxy.a;
let b = proxy.b;
let a = proto.a;
let b = proto.b;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Basic | |
Proxy | |
Proto |
Test name | Executions per second |
---|---|
Basic | 364329024.0 Ops/sec |
Proxy | 50458108.0 Ops/sec |
Proto | 365625952.0 Ops/sec |
Let's dive into the provided benchmark definition and explain what's being tested.
Benchmark Definition:
The test is comparing three approaches to access nested properties of an object:
basic.a
and basic.b
)proxy.a
and proxy.b
) to gain access to the nested propertiesObject.setPrototypeOf()
to set up a prototype chain for the object (proto.a
and proto.b
)Options Compared:
The test is comparing the performance of these three approaches:
Library Used:
The test uses the built-in Proxy
constructor in JavaScript, which allows for creating objects that provide custom behavior for certain property access operations. The Object.setPrototypeOf()
method is also used to set up a prototype chain for an object.
Special JS Feature/Syntax:
This benchmark does not use any special JavaScript features or syntax beyond what's standard in the language.
Other Alternatives:
If you're interested in exploring alternative approaches, here are a few options:
basic.a.b
) instead of bracket notation.basic['a']
and proxy['b']
).function getA() { return basic.a; }
).Keep in mind that these alternatives may have different performance characteristics or trade-offs, depending on the specific use case.
Benchmark Preparation Code:
The script preparation code defines three objects:
basic
: An object with two properties (a
and b
) set to 1 and 2, respectively.proxy
: A Proxy object created over an object with the same properties as basic
. The proxy uses a getter function to return the value of each property when accessed.proto
: An object that inherits from an empty object using Object.setPrototypeOf()
. The prototype is set up to have the same properties as basic
.The HTML preparation code is not provided, suggesting that it's not necessary for this specific benchmark.
Latest Benchmark Result:
The latest benchmark results show that:
These results suggest that using Object.setPrototypeOf()
to set up a prototype chain for the object provides the best performance in this specific benchmark.