object = { value: 'data', getData() {return this.value;} };
Object.defineProperty(object, 'dataProperty', {
get() { return this.value; }
});
proxy = new Proxy(object, {})
proxyWithHandler = new Proxy(object, {
get(target, prop, receiver) {
return target[prop]
}
})
object.value;
proxy.value
proxyWithHandler.value
object.getData();
object.dataProperty;
--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 |
Test name | Executions per second |
---|---|
Object access | 12856777.0 Ops/sec |
Proxy access | 10268416.0 Ops/sec |
Proxy with get handler access | 9732634.0 Ops/sec |
Use of simple getter | 12566425.0 Ops/sec |
Use of defineProperty | 12752992.0 Ops/sec |
I'll break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of accessing an object property using three different approaches:
object.value
).proxy.value
).proxyWithHandler.value
).Options Compared
The benchmark compares the performance of each approach, which can be summarized as follows:
Pros and Cons
Here are the pros and cons of each approach:
Libraries and Special JS Features
In this benchmark, there is no specific library used, but the Proxy
object is a built-in JavaScript feature introduced in ECMAScript 2015 (ES6).
Other Considerations
When working with Proxies, it's essential to consider the following:
Alternatives
If you need alternative approaches or have different requirements, consider the following options:
json-store
or objection.js
, which provide features similar to Proxies but with additional functionality.In summary, the benchmark tests the performance of accessing an object property using three approaches: direct object access, proxy access, and proxy with get handler access. The pros and cons of each approach are discussed, as well as considerations for working with Proxies in JavaScript.