object = { value: 'data' };
proxy = new Proxy(object, {})
proxyWithHandler = new Proxy(object, {
get(target, prop, receiver) {
return Reflect.get(target, prop, receiver)
}
})
object.value;
proxy.value
proxyWithHandler.value
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object access | |
Proxy access | |
Proxy with get handler access |
Test name | Executions per second |
---|---|
Object access | 227172768.0 Ops/sec |
Proxy access | 79199672.0 Ops/sec |
Proxy with get handler access | 48957196.0 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition
The benchmark is defined in JSON format, which includes:
Name
: The name of the benchmark, "Access to Proxy vs Object".Description
: An empty string, indicating that no description is provided for this benchmark.Script Preparation Code
: A JavaScript code snippet that creates an object with a value, and then creates two proxies: one using the built-in Proxy
constructor without any handlers (proxy
), and another using the same object but with a custom get handler (proxyWithHandler
). The script also declares variables for these proxies.Html Preparation Code
: An empty string, indicating that no HTML code is needed to prepare for this benchmark.The purpose of this benchmark is to compare the performance of accessing an object's property directly (using dot notation) versus using a proxy. A proxy is an object that provides access to its target object, but with additional functionality that can be specified during creation.
Options Compared
In this benchmark, two options are compared:
value
property of the object using dot notation (object.value
).value
property. There are two variations:proxy.value
: Using the built-in Proxy
constructor without any handlers.proxyWithHandler.value
: Using the same object but with a custom get handler.Pros and Cons
Here's a brief overview of the pros and cons of each approach:
Library
In this benchmark, the Reflect
library is implicitly used through the Reflect.get()
function in the custom get handler. The Reflect
library provides a set of functions that can be used to perform operations on objects without modifying their behavior.
Special JavaScript Feature/Syntax
The use of the built-in Proxy
constructor and the custom get handler (proxyWithHandler
) is an example of using a proxy in JavaScript. Proxies are a feature introduced in ECMAScript 2015 (ES6) that allow for dynamic and customizable objects.
Other Alternatives
For this specific benchmark, other alternatives to compare would be:
Object.getPrototypeOf()
or Object.create()
to create a proxy-like object.Object.defineProperty()
.get()
function to access properties.However, these alternatives might not provide the same level of customization and flexibility as using a built-in Proxy
constructor or a custom get handler.