Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Plain vs Getter/Setter vs Proxy
(version: 0)
Comparing performance of:
Plain vs Getter/Setter vs Proxy
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var test = 0; var plain = {}; var getterSetter = { get test() { return this._test; }, set test(value) { return this._test = value; } } var proxyData = {}; var proxy = new Proxy(proxyData, { get(target, prop) { return target[prop]; }, set(target, prop, value) { return target[prop] = value; } });
Tests:
Plain
plain.test = Math.random(); test += plain.test;
Getter/Setter
getterSetter.test = Math.random() test += getterSetter.test;
Proxy
proxy.test = Math.random(); test += proxy.test;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Plain
Getter/Setter
Proxy
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/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Plain
7797751.0 Ops/sec
Getter/Setter
4268470.5 Ops/sec
Proxy
5059213.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the JavaScript microbenchmark you provided. **What is being tested?** The benchmark is designed to compare three approaches: 1. **Plain**: Direct access to an object property using dot notation (e.g., `plain.test`). 2. **Getter/Setter**: Using a getter and setter function to access and modify an object property (e.g., `getterSetter.test`). 3. **Proxy**: Using the `Proxy` API to create a proxy object that intercepts read and write operations on its properties. **Options comparison** The three approaches differ in how they handle property access and modification: * **Plain**: Directly accesses the `test` property of the `plain` object using dot notation. * **Getter/Setter**: Uses a getter function (`get test()`) to return the value of the `test` property and a setter function (`set test(value)`) to set its value. This introduces an additional layer of indirection. * **Proxy**: Creates a proxy object that intercepts read and write operations on its properties using the `Proxy` constructor. When accessed, the proxy returns the value of the target property. **Pros and Cons** Here are some pros and cons for each approach: * **Plain**: + Pros: Simple and direct access to properties. + Cons: No error handling or logging for invalid property names or null values. * **Getter/Setter**: + Pros: Provides a way to log or validate the value being set, but introduces an additional layer of indirection. + Cons: Requires more code to define the getter and setter functions, and can be slower due to the extra function calls. * **Proxy**: + Pros: Allows for fine-grained control over property access and modification, including logging or validation. + Cons: Can be slower than plain access due to the additional overhead of creating a proxy object. **Library and syntax** The benchmark uses the `Proxy` API, which is part of the JavaScript standard library. It does not use any external libraries for this specific test case. **Special JS feature or syntax** There are no special JavaScript features or syntaxes used in this benchmark beyond what's already explained. Now that we've explored the benchmark, let's talk about other alternatives: * **Use a Just-In-Time (JIT) compiler**: If you're interested in optimizing your code for specific environments or browsers, using a JIT compiler like V8 or SpiderMonkey can help. * **Profile-guided optimization**: Instead of relying solely on benchmarking, consider profiling your application to identify performance bottlenecks and optimize those areas specifically. * **Use existing benchmarks**: MeasureYourCode (like MeasureThat.net) is just one example; there are many other benchmarking tools and resources available that can help you compare the performance of different JavaScript approaches. In conclusion, this benchmark provides a solid comparison between three fundamental ways to access properties in JavaScript. By understanding the pros and cons of each approach, you'll be better equipped to choose the best method for your specific use case.
Related benchmarks:
ES6 Proxy vs JS Object get/set
ES6 Proxy vs JS Object get/set vs get/set Function
get vs proxy get v2
Proxy.get(prop) vs obj[prop]
Plain object access vs Proxy vs Property
Comments
Confirm delete:
Do you really want to delete benchmark?