Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
nested getters + closure vs. function
(version: 1)
Comparing performance of:
old vs new
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const obj = { a: 1 }; const company1 = { get ab() { return (key) => { return obj[key]; } } } const api1 = { get ab() { return company1.ab; } } const company2 = { ab(key) { return obj[key]; } } const api2 = { ab: company2.ab, }
Tests:
old
api1.ab('a')
new
api2.ab('a')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
old
new
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
old
769272512.0 Ops/sec
new
932706048.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you're looking at compares two different approaches to accessing a value in a JavaScript object: the first method uses a getter with a closure, and the second uses a direct method call. ### Approaches Tested 1. **Using a Getter with Closure (Old Approach)**: - **Code**: ```javascript const company1 = { get ab() { return (key) => { return obj[key]; } } }; const api1 = { get ab() { return company1.ab; } }; ``` - **Method**: This approach defines a getter, `ab`, on `company1`. When accessed, it returns a function that takes a key and retrieves a value from `obj`. The getter in `api1` simply returns the `ab` from `company1`. 2. **Using a Regular Function (New Approach)**: - **Code**: ```javascript const company2 = { ab(key) { return obj[key]; } }; const api2 = { ab: company2.ab, }; ``` - **Method**: This approach defines a method, `ab`, directly on `company2` which takes a key and returns the corresponding value from `obj`. ### Comparison & Performance - **Execution Speed**: - The test results indicate that the new approach (`api2.ab('a')`) has a significantly higher execution speed, with **149,022,752 executions per second** compared to **38,068,040 executions per second** for the old approach. This suggests that using a direct method call is more efficient than using a getter that returns a closure. ### Pros and Cons 1. **Getter with Closure (Old Approach)**: - **Pros**: - Provides a level of abstraction, as users can interact with it as if it were a property. - Closures allow maintaining a private state. - **Cons**: - Generally slower due to the additional function call overhead. - Increased complexity in understanding the code flow. 2. **Regular Function (New Approach)**: - **Pros**: - Faster execution as it eliminates the overhead of closures. - Simpler and more straightforward code, easier to maintain. - **Cons**: - May have less intuitive property-like access, depending on the context of use. ### Other Considerations - **Use Case Sensitivity**: The choice between using getters or regular functions can depend on specific use cases. If you want to maintain state or encapsulate behavior, getters with closures can be beneficial. However, for performance-critical applications or scenarios where you simply need to access data, regular functions are preferable. - **Memory Consumption**: The closure approach could lead to higher memory consumption because every time the getter is called, a new function is created. This could be a concern in environments where performance and memory efficiency are critical. ### Alternatives - There are other methods to access object properties, such as using simple object destructuring or direct property access. - You could also consider libraries or tools like Lodash, which offer a variety of utility functions to work with objects, providing both simple access and powerful manipulation. However, when it comes to pure performance, the simplest approach (like the method used in the "new" test) will generally outperform the use of such libraries for simple access patterns. In conclusion, when deciding between these approaches, consider the specific needs of your application: readability, maintainability, and performance. For straightforward data access, a direct method (like in the new approach) is usually the best choice.
Related benchmarks:
abcddef
Random Getters
Lodash extend vs Object.assign
HashMapLoop
isEmptyObject: keys vs for in 2
Creating a partial object: destructuring vs. property picking
IIFE assignment vs mutable assignment
Loadash vs underscore simple and complex
Object creation vs assign
Comments
Confirm delete:
Do you really want to delete benchmark?