Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
property access: object with define property vs Proxy get vs plain object
(version: 0)
Comparing performance of:
Proxy vs defineProperty vs getter vs Control (plain object access)
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.queryResults = queryResults; 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
Control (plain object access)
window.queryResults.d.a + 5
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Proxy
defineProperty
getter
Control (plain object access)
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Proxy
41264308.0 Ops/sec
defineProperty
198726096.0 Ops/sec
getter
91948256.0 Ops/sec
Control (plain object access)
209872000.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The test compares the performance of three approaches to access a property in an object: 1. `Proxy` (using `window.trackedResultsProxy`) 2. `defineProperty` (using `window.trackedResultsDefineProperty`) 3. Direct access (plain object access) (`window.queryResults`) **Options Compared** The two custom options (`Proxy` and `defineProperty`) are compared to the most basic option, direct access. * `Proxy`: uses a Proxy object to intercept property accesses. * `defineProperty`: uses the `Object.defineProperty()` method to define properties on an object. * Direct access: simply accesses the property directly on the original object (`queryResults`). **Pros and Cons** Here's a brief summary of each approach: * **Proxy**: Pros: + Can provide more control over property behavior (e.g., transformations, validation). + Can be more efficient for complex or nested properties. * Cons: + Requires more overhead to set up the Proxy object. + Might not be supported in older browsers. * **defineProperty**: Pros: + Provides a more explicit way to define property behavior (e.g., configurable, enumerable). + Can be more efficient for simple properties. * Cons: + May require more code to set up and maintain the properties. + Might not be as flexible as Proxy for complex cases. * **Direct Access**: Pros: + Simplest approach, requiring minimal setup. + Most likely to work in all browsers. **Library Usage** In this benchmark, two libraries are used: 1. `Proxy`: The built-in JavaScript `Proxy` object is used to create a Proxy instance. 2. No other library is explicitly mentioned; however, the `Object.defineProperty()` method is part of the standard JavaScript API. **Special JS Features or Syntax** None of these options rely on special JavaScript features or syntax (e.g., ES6+ classes, generators, etc.). **Other Alternatives** If you need to measure the performance of other approaches to access properties in objects, consider exploring alternative methods: * Using `Object.getPrototypeOf()` and chaining property accesses. * Utilizing a library like Lodash's `get()` method for more complex cases. * Implementing a custom iterator or generator function for sequential access. Keep in mind that each approach may have its own trade-offs, pros, and cons, which can impact performance.
Related benchmarks:
object with define property vs Proxy get
property access: object with define property vs Proxy get
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?