Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Cached Getter (class) vs Getter vs Proxy
(version: 0)
Comparing performance of:
getter / setter vs proxy vs plain object vs class getter vs cached class getter
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
plainObj = {test: 1}; handler = { get: (target, prop) => (target[prop]), }; proxy = new Proxy(plainObj, handler); class ClassGetterSetter { constructor() { this._test = 1; } get test() { return this._test; } } class CachedGetterSetter { constructor() { this._test = 1; } get test() { var val = this._test; //this.test = val; Object.defineProperty(this, 'test', { value: val, }); return val; } } objWithGetter = { _test: 1, get test() { return this._test; }, }; objWithClass = new ClassGetterSetter(); objWithCachedClass = new CachedGetterSetter(); var a = 0; objWithCachedClass.test;
Tests:
getter / setter
a += objWithGetter.test;
proxy
a += proxy.test;
plain object
a += plainObj.test;
class getter
a += objWithClass.test;
cached class getter
a += objWithCachedClass.test;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
getter / setter
proxy
plain object
class getter
cached class getter
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
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 provided JSON defines a benchmark named "Cached Getter (class) vs Getter vs Proxy" that compares the performance of three different approaches: 1. **Proxy**: Using JavaScript's built-in `Proxy` object to create a proxy object. 2. **Plain Object**: Accessing an object directly using its property name. 3. **Class Getter Setter**: Creating a class with getter and setter methods. **Options Compared** The benchmark tests the performance of each option by executing the following lines of code: ```javascript a += objWithGetter.test; ``` ```javascript a += proxy.test; ``` ```javascript a += plainObj.test; ``` ```javascript a += objWithClass.test; ``` ```javascript a += objWithCachedClass.test; ``` **Pros and Cons** * **Proxy**: Pros: Provides a way to access an object's property without directly accessing the underlying object. Can be useful for security, debugging, or performance optimization purposes. Cons: May introduce additional overhead due to the creation of a proxy object. * **Plain Object**: Pros: Directly accesses the object's property without any intermediate step. Often used for simplicity and readability. Cons: Does not provide any benefits in terms of security or performance over directly accessing the property. * **Class Getter Setter**: Pros: Provides a way to encapsulate data access using getter and setter methods, which can be useful for encapsulation, security, and data validation purposes. Cons: May introduce additional overhead due to the creation of an instance. **Library and Purpose** The `Proxy` object is a built-in JavaScript object that allows creating proxy objects, which are essentially wrapper objects around another object. In this benchmark, the `Proxy` object is created with a handler object that defines the behavior for accessing properties on the underlying object. In this case, the handler sets the value of the property to be accessed to be the same as the original value. **Special JS Feature** This benchmark uses JavaScript's feature called " getter and setter methods" which allow encapsulation and data validation in classes. The `Class Getter Setter` option demonstrates how to use these features to access an object's data through controlled interfaces. However, it seems like this benchmark is using the getter method without assigning its return value back to itself or any other variable, so that `this.test = val;` line is commented out which might reduce some of the overhead that would be caused by such assignments.
Related benchmarks:
Prototypal property access vs passing cached value
Proxy.get(prop) vs obj[prop]
Calling method of a proxy vs object
Proxy vs defineProperty, assign to local
Comments
Confirm delete:
Do you really want to delete benchmark?