Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Global vs Local function
(version: 0)
Comparing performance of:
Local Function vs Global Function
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var examples = []; for(let i = 0; i < 100; i++) { examples.push({ attr: "Hello", }); } function innerFn(obj) { function hasAttr(obj) { return typeof obj === "object" && "attr" in obj; } if(hasAttr(obj)) return obj.attr + " world!"; else return null; } function hasAttr(obj) { return typeof obj === "object" && "attr" in obj; } function outerFn(obj) { if(hasAttr(obj)) return obj.attr + " world!"; else return null; }
Tests:
Local Function
for(const example of examples) { console.log(innerFn(example)); }
Global Function
for(const example of examples) { console.log(outerFn(example)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Local Function
Global Function
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 and its results. **Benchmark Overview** The benchmark measures the performance difference between two approaches: using a global function and using a local function within a loop. The script prepares an array of objects with a single attribute "attr" and then iterates over this array, logging the result of calling either `innerFn` (the local function) or `outerFn` (the global function) on each object. **Options Compared** The two options being compared are: 1. **Global Function**: This approach defines a global function `outerFn` outside the loop. The function checks if an object has an "attr" property, and if so, returns the value of that attribute concatenated with " world!". If not, it returns null. 2. **Local Function**: This approach defines a local function `innerFn` within the loop. It also checks if an object has an "attr" property and returns the same result as the global function. **Pros and Cons** **Global Function:** Pros: * Easier to understand and maintain, as it's defined outside the loop. * Less memory overhead, since it's only instantiated once. Cons: * May lead to slower performance due to the extra function call overhead when using a local variable (e.g., `var result = outerFn(example);`). **Local Function:** Pros: * More efficient, as it avoids the extra function call overhead. * Better suited for use within a loop, as it's only defined in scope. Cons: * May be harder to understand and maintain, especially for complex functions. * May incur additional memory overhead due to the creation of a new local variable. **Library/Functionality Used** None are explicitly mentioned in the provided code. However, the `typeof` operator is used to check if an object has a specific property (`"attr"`), which is a built-in JavaScript feature. **Special JS Features/Syntax** The benchmark uses a few ES6 features: * `const` declarations (e.g., `const example of examples`) * `for...of` loops * Template literals (e.g., `${obj.attr} world!`) These features are widely supported in modern browsers, but may not be compatible with older JavaScript engines. **Alternative Approaches** Other possible approaches to benchmarking this scenario could include: 1. Using a static function instead of an outer or inner function. 2. Comparing the performance of using `in` operator vs. `hasAttr` function. 3. Adding more complex logic to the functions being compared (e.g., additional checks, loops, etc.). However, these alternatives would likely introduce unnecessary complexity and may not provide meaningful insights into the specific performance differences between global and local functions in this context. In summary, the provided benchmark effectively compares the performance of using a global function versus a local function within a loop. The results highlight the benefits of using local variables to avoid extra function call overhead, while also considering factors like maintainability and memory efficiency.
Related benchmarks:
Bable vs Native
.bind() vs function
.bind() vs function
hasOwnProperty vs hasOwnProperty .call
eval inderect call vs new Function
Comments
Confirm delete:
Do you really want to delete benchmark?