Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
class set get (getters, function, direct)
(version: 0)
Comparing performance of:
getter vs Getter function vs Directly get
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
class Class { constructor () { this._val = 1 } get value() { return this._val } set value(x) { this._val = x } getValue() { return this._val } setValue(x) { this.val = x } } var i = 0; var cls = new Class() var wtf
Tests:
getter
cls.value = i++; wtf = cls.value;
Getter function
cls.setValue(i++); wtf = cls.getValue();
Directly get
cls._val = i++; wtf = cls._val;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
getter
Getter function
Directly get
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is a JavaScript class with three properties: `value`, `getValue()`, and `setValue()`. The constructor initializes `this._val` to 1. The `value` property has getter and setter methods, while the `_val` variable can be directly accessed. The script preparation code creates an instance of this class (`cls`) and assigns a value to it (`wtf`). This setup allows us to test different ways of accessing the class's internal state. **Options Compared** Three options are compared: 1. **Getter**: `cls.value` 2. **Getter function**: `cls.getValue()` 3. **Directly get**: `cls._val` These three approaches have different pros and cons: * **Getter**: This is a syntactic sugar for accessing the class's internal state through a property name (`value`). It provides a more intuitive way of accessing the value but may not be as fast as directly accessing the `_val` variable. * **Getter function**: This approach involves calling a named function to get the value. It might be slower than directly accessing `_val` due to the overhead of function calls. * **Directly get**: This is the most straightforward way of accessing the class's internal state by using the variable name (`_val`). However, it bypasses any potential getter or setter logic. **Library and Special Features** In this benchmark, there are no external libraries used. Additionally, JavaScript features such as `const`, `let`, `for...of` loops, or arrow functions are not explicitly mentioned in the benchmark definition code. **Other Alternatives** If we were to add more test cases, we might consider including: * **Setter**: Testing how accessing the class's internal state through a setter method (`cls.setValue(x)`) compares to directly accessing `_val`. * **Property access with `in` operator**: Comparing the performance of accessing properties using the dot notation (`cls.value`) versus the bracket notation (`cls['value']`). * **Property access using a custom property accessor**: Implementing and testing a custom property accessor (e.g., a function that returns the value) to compare its performance with the existing getter methods. The specific alternatives will depend on the goals of the benchmark and the requirements for comparison.
Related benchmarks:
ES6 property (get/set) & getter/setter function & direct access to object attribute
ES6 property get & getter function & direct access to object attribute
ES6 property (get/set) & getter/setter function vs direct access to object attribute (private and not)
ES6 property (get/set) & getter/setter function vs direct access to object attribute (private and not) v2
ES6 property (get/set) & getter/setter function vs direct access vs closure
Comments
Confirm delete:
Do you really want to delete benchmark?