Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object method scope lookup
(version: 0)
Comparing performance of:
hoisted global vs use arrow vs use own
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arrow = x => x + 1; var object = { useHoistedGlobal(x) { return hoistedGlobal(x); }, useArrow(x) { return arrow(x); }, useOwn(x) { return this._own(x); }, _own(x) { return x + 1; } } function hoistedGlobal(x) { return x + 1; } var n = 420;
Tests:
hoisted global
n = object.useHoistedGlobal(1);
use arrow
n = object.useArrow(1);
use own
n = object.useOwn(1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
hoisted global
use arrow
use own
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
hoisted global
2484165.0 Ops/sec
use arrow
2535650.0 Ops/sec
use own
3562874.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. **Benchmark Definition JSON:** The provided benchmark definition represents an object method scope lookup test case. It defines a JavaScript function `arrow` that takes an input `x` and returns `x + 1`. This function is then used within another object, which has three methods: 1. `useHoistedGlobal(x)`: This method calls the global variable `hoistedGlobal(x)` with the input `x`. 2. `useArrow(x)`: This method directly uses the defined arrow function `arrow(x)` to perform the calculation. 3. `useOwn(x)`: This method calls a local property `_own(x)` of the object, which returns `x + 1`. The benchmark script prepares these methods and assigns them to the object, along with a global variable `n` initialized to `420`. The objective is to measure the performance difference between using the global variable, the arrow function, or the own method (`useOwn(x)`) to perform the calculation. **Options Compared:** The benchmark compares three different approaches: 1. **Hoisted Global**: Using the global variable `hoistedGlobal(x)` to perform the calculation. 2. **Arrow Function**: Directly using the defined arrow function `arrow(x)` to perform the calculation. 3. **Own Method (`useOwn(x)`)**: Calling a local property `_own(x)` of the object to perform the calculation. **Pros and Cons:** 1. **Hoisted Global**: * Pros: Simple, well-established syntax, easy to read and maintain. * Cons: May lead to performance issues due to global variable lookups, potential for conflicts with other code. 2. **Arrow Function**: * Pros: Concise, expressive syntax, good performance characteristics. * Cons: Limited compatibility with older browsers or environments that don't support arrow functions. 3. **Own Method (`useOwn(x)`)**: * Pros: Encapsulation, encapsulation of the calculation logic within the object, potential for better performance due to reduced scope lookups. * Cons: Less intuitive syntax for some developers, may require additional code to implement. **Library and Special JS Features:** The benchmark uses a JavaScript function `hoistedGlobal(x)` which is not part of any standard library. This function is likely used to simulate a global variable lookup in a hypothetical scenario. There are no special JavaScript features (e.g., async/await, destructuring) used in this benchmark. **Other Alternatives:** To write similar benchmarks, you could consider testing the following alternative approaches: 1. **Closure**: Using a closure to encapsulate the calculation logic within an object or function. 2. **Function Expression**: Using a function expression instead of a regular function declaration. 3. **Generator Functions**: Using generator functions for asynchronous calculations. Keep in mind that the choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
closure lookup
Class instance method lookup vs function-created object method lookup (fork)
Noop vs new arrow function
Noop vs new arrow function calls
Comments
Confirm delete:
Do you really want to delete benchmark?