Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Proxy vs Getter
(version: 0)
Comparing performance of:
Raw access vs Function vs Getter vs Proxy
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
obj = { value: "Value", getData() { return obj.value; }, get data() { return obj.value; } }; proxy = new Proxy({}, { get(_, prop) { if(prop !== "value") return; return obj.value; } });
Tests:
Raw access
obj.value;
Function
obj.getData();
Getter
obj.data;
Proxy
proxy.value;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Raw access
Function
Getter
Proxy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Raw access
77061160.0 Ops/sec
Function
55841024.0 Ops/sec
Getter
45222720.0 Ops/sec
Proxy
22499998.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and discussed. **Benchmark Purpose:** The benchmark compares the performance of accessing object properties using three different methods: 1. **Raw access**: Directly accessing the `value` property on the `obj` object. 2. **Function call**: Calling the `getData()` function to retrieve the value. 3. **Getter method**: Using the `get data()` getter method to access the value. **Library and Syntax:** The benchmark uses JavaScript's Proxy object to create a proxy for the `obj` object. A proxy is an object that acts as a middleman between other objects, allowing you to customize access to its properties. In this case, the proxy is used to intercept property accesses on the `proxy` object. The `get` trap is defined to return the value of the `value` property when accessed directly (`proxy.value`). **Special Features and Syntax:** The benchmark uses a syntax feature called **computed property names**, which allows you to use strings as property names in JavaScript. For example, in the line `obj.data = { ... }`, the string `"data"` is used as a computed property name. This feature was introduced in ECMAScript 2015 (ES6) and provides a way to create dynamic property names at runtime. **Pros and Cons of Different Approaches:** Here's a brief summary of the pros and cons of each approach: * **Raw access**: Fastest, but may involve unnecessary computations or memory accesses. + Pros: Simple, efficient. + Cons: May not be thread-safe, can lead to unexpected behavior if used with mutable objects. * **Function call**: Slightly slower than raw access, but provides a way to encapsulate logic and make the code more modular. + Pros: More readable, maintainable, and reusable. Provides a clear separation of concerns. + Cons: May introduce overhead due to function calls, which can lead to performance issues in certain scenarios. * **Getter method**: Slower than raw access and function calls, but provides a way to customize property access and make the code more expressive. + Pros: More flexible, allows for customization of property access. + Cons: May introduce unnecessary overhead due to function call and property lookup. **Other Alternatives:** If you want to explore alternative approaches, here are some options: * **Dynamically accessed properties**: You can use the `in` operator or the bracket notation (`obj['value']`) to access properties dynamically. However, this approach may lead to slower performance due to the overhead of dynamic property lookup. * **Object property descriptors**: You can use object property descriptors (e.g., `Object.defineProperty()`) to customize property access and make the code more expressive. This approach provides more control over property behavior but may also introduce additional complexity. Keep in mind that each approach has its own trade-offs, and the choice ultimately depends on your specific use case, performance requirements, and coding style preferences.
Related benchmarks:
ES6 Proxy vs JS Object get/set
ES6 Proxy vs JS Object get/set vs get/set Function
Access to Proxy vs Object Another one, nother one
Proxy.get(prop) vs obj[prop]
Access to Proxy vs Object vs Getters fixed
Comments
Confirm delete:
Do you really want to delete benchmark?