Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IIFE vs direct call vs no function
(version: 0)
Comparing performance of:
IIFE vs Function call vs Direct code
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function sum(x) { var res = 0; for (var i = 0; i < x; ++i) res += i; return res; }
Tests:
IIFE
(function(x) { var res = 0; for (var i = 0; i < x; ++i) res += i; return res; })(100)
Function call
sum(100)
Direct code
var x = 100; var res = 0; for (var i = 0; i < x; ++i) res += i;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
IIFE
Function call
Direct code
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IIFE
22763706.0 Ops/sec
Function call
22586506.0 Ops/sec
Direct code
22935814.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net! The provided JSON represents three test cases that compare different approaches to calling a function in JavaScript: Immediately Invoked Function Expression (IIFE), direct function call, and no function wrapper. **Benchmark Definition:** The benchmark definition is a simple script that calculates the sum of numbers from 0 to `x`. This script will be used as a common base for all three test cases. The script preparation code is identical across all three test cases: ```javascript function sum(x) { var res = 0; for (var i = 0; i < x; ++i) res += i; return res; } ``` However, the execution of this function is wrapped in different wrappers to compare their performance. These wrappers are used in the test cases below. **Individual Test Cases:** 1. **IIFE (Immediately Invoked Function Expression):** ```javascript (function(x) { var res = 0; for (var i = 0; i < x; ++i) res += i; return res; })(100); ``` The IIFE is a special type of function that is executed immediately after it's defined. This approach creates a new scope and has its own set of variables, which can lead to performance benefits due to reduced global variable lookups. Pros: * Reduced global variable lookups * Improved code organization Cons: * Can be more complex to debug * May have performance overhead due to the creation of a new scope 2. **Direct Function Call:** ```javascript sum(100); ``` This approach simply calls the `sum` function directly, without any wrappers. Pros: * Simple and straightforward code * No performance overhead due to wrapper creation Cons: * May have slower performance due to global variable lookups * Less organized code compared to IIFE 3. **Direct Code:** ```javascript var x = 100; var res = 0; for (var i = 0; i < x; ++i) res += i; ``` This approach is similar to the direct function call, but without creating a named function `sum`. Instead, it uses a block of code that calculates the sum directly. Pros: * Simple and straightforward code * No performance overhead due to wrapper creation Cons: * May have slower performance due to global variable lookups * Less organized code compared to IIFE **Library:** There is no specific library mentioned in the benchmark definition or test cases. The `sum` function is a simple script that calculates the sum of numbers from 0 to `x`. **Special JS Feature/Syntax:** None of the test cases use any special JavaScript features or syntax beyond basic syntax like `for` loops, variable declarations, and function calls. **Other Alternatives:** If you're looking for alternatives to MeasureThat.net, here are a few options: 1. jsperf (now deprecated) 2. Google's Benchmarking API 3. V8 benchmarking 4. JavaScriptPerf These tools provide similar functionality to MeasureThat.net and allow developers to compare the performance of different code snippets in various browsers. In conclusion, the MeasureThat.net benchmark highlights the importance of considering function call overhead and global variable lookups when optimizing JavaScript code. By using an IIFE, you can reduce these overheads and improve performance, but at the cost of increased complexity. The other test cases demonstrate simpler approaches that may be suitable for specific use cases, but may not offer the same level of performance as the IIFE wrapper.
Related benchmarks:
eval vs new Function v5
IIFE vs eval()
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
Summing with EcmaScript6 Int16Array vs regular JS array
Comments
Confirm delete:
Do you really want to delete benchmark?