Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
property access: object with define property vs Proxy get
(version: 0)
Comparing performance of:
Proxy vs defineProperty vs getter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const queryResults = { a: {}, b: [2, 3, 4], c: 3, d: { a: 1, b: [], }, }; window.trackedResultsProxy = new Proxy(queryResults, { get(target, prop, receiver) { return Reflect.get(target, prop, receiver); } }); window.trackedResultsDefineProperty = {}; for (const key of Object.keys(queryResults)) { Object.defineProperty(trackedResultsDefineProperty, key, { configurable: false, enumerable: true, get() { return queryResults[key]; }, }); } window.trackedResultsGetter = { get d() { return queryResults.d; } }
Tests:
Proxy
window.trackedResultsProxy.d.a + 5
defineProperty
window.trackedResultsDefineProperty.d.a + 5
getter
window.trackedResultsGetter.d.a + 5
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Proxy
defineProperty
getter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Proxy
4833679.5 Ops/sec
defineProperty
5918428.5 Ops/sec
getter
5462940.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark measures the performance of accessing a property with different approaches: 1. `window.trackedResultsProxy`: A Proxy object, which is a special type of object that allows you to intercept and modify access to its properties. 2. `window.trackedResultsDefineProperty`: An object created using `Object.defineProperty()`, which defines a property on an object. 3. `window.trackedResultsGetter`: An object with a getter property that returns the value of another property. **Options Compared:** * **Proxy**: The Proxy object is used to intercept and modify access to its properties. When you access a property on the Proxy object, it calls the `get()` method specified in the Proxy's `get()` trap. * **defineProperty**: The `Object.defineProperty()` method creates a new property on an object. In this case, a property named "d" is created on `window.trackedResultsDefineProperty` with a getter function that returns the value of another property. * **Getter**: The `window.trackedResultsGetter` object has a getter property named "d" that returns the value of another property. **Pros and Cons:** * **Proxy**: + Pros: - Can be used to intercept and modify access to properties at runtime. - Can provide more flexibility and customization than `defineProperty`. + Cons: - May have performance overhead due to the additional indirection. * **defineProperty**: + Pros: - Creates a new property on an object that is faster than creating it dynamically using a Proxy. - Less complex than using a Proxy. + Cons: - Less flexible and customizable than using a Proxy. * **Getter**: + Pros: - Is a simple way to expose a value from another object. + Cons: - Does not provide any flexibility or customization options. **Library Usage:** The `Reflect` library is used in the `get()` trap of the Proxy object. It provides functions for working with objects, such as `Reflect.get()`, which returns the value of a property on an object. **Special JavaScript Feature/Syntax:** The use of the `Proxy` object and its `get()` trap introduces some advanced JavaScript concepts: * **Prototypal Inheritance**: The Proxy object inherits from the native Object prototype. * **Property Traps**: The `get()` trap allows you to intercept and modify access to properties on an object. **Other Alternatives:** * Instead of using a Proxy, you could use a plain object and define getters and setters manually. * You could use the `Object.create()` method to create an object that inherits from another object. * You could use a library like Lodash or Ramda to provide more flexible and customizable ways to work with objects. In summary, the benchmark measures the performance of accessing a property using different approaches: Proxy, defineProperty, and Getter. The Proxy approach provides flexibility and customization options but may have performance overhead, while defineProperty is faster but less flexible.
Related benchmarks:
object with define property vs Proxy get
property access: object with define property vs Proxy get vs plain object
property access: object with define property vs Proxy get vs plain object 2
property access: object with define property vs Proxy get vs plain object vs methods
Comments
Confirm delete:
Do you really want to delete benchmark?