Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Closure overhead vs function overhead vs inline
(version: 0)
Runs a pseudo random number generator in a loop. To determine overhead from IIFE in a loop versus function call overhead
Comparing performance of:
Inline vs IIFE vs External function
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var NUM_ITERATIONS = 1000000 var state = [987654321, 987654321, 987654321, 987654321]; var baz_inner = (state) => { state[0] = ((state[0] & 0xFFFFFFFE) << 18) ^ (((state[0] << 6) ^ state[0]) >> 13); state[1] = ((state[1] & 0xFFFFFFF8) << 2) ^ (((state[1] << 2) ^ state[1]) >> 27); state[2] = ((state[2] & 0xFFFFFFF0) << 7) ^ (((state[2] << 13) ^ state[2]) >> 21); state[3] = ((state[3] & 0xFFFFFF80) << 13) ^ (((state[3] << 3) ^ state[3]) >> 12); }
Tests:
Inline
state = [987654321, 987654321, 987654321, 987654321]; for (let i = 0; i < NUM_ITERATIONS; i++) { state[0] = ((state[0] & 0xFFFFFFFE) << 18) ^ (((state[0] << 6) ^ state[0]) >> 13); state[1] = ((state[1] & 0xFFFFFFF8) << 2) ^ (((state[1] << 2) ^ state[1]) >> 27); state[2] = ((state[2] & 0xFFFFFFF0) << 7) ^ (((state[2] << 13) ^ state[2]) >> 21); state[3] = ((state[3] & 0xFFFFFF80) << 13) ^ (((state[3] << 3) ^ state[3]) >> 12); }
IIFE
state = [987654321, 987654321, 987654321, 987654321]; for (let i = 0; i < NUM_ITERATIONS; i++) { (() => { state[0] = ((state[0] & 0xFFFFFFFE) << 18) ^ (((state[0] << 6) ^ state[0]) >> 13); state[1] = ((state[1] & 0xFFFFFFF8) << 2) ^ (((state[1] << 2) ^ state[1]) >> 27); state[2] = ((state[2] & 0xFFFFFFF0) << 7) ^ (((state[2] << 13) ^ state[2]) >> 21); state[3] = ((state[3] & 0xFFFFFF80) << 13) ^ (((state[3] << 3) ^ state[3]) >> 12); })(); }
External function
state = [987654321, 987654321, 987654321, 987654321]; for (let i = 0; i < NUM_ITERATIONS; i++) { baz_inner(state); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Inline
IIFE
External 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 JSON and explain what is being tested. **Benchmark Overview** The benchmark compares the performance of three approaches: 1. **Inline**: The code is inlined, meaning it's directly inserted into the loop without any function calls or declarations. 2. **IIFE (Immediately Invoked Function Expression)**: A function is defined immediately and then called without any arguments. This creates a new scope and executes the code inside the IIFE as soon as it's created. 3. **External function**: The `baz_inner` function is declared separately from the loop, and its name is passed to the loop as an argument. **Pros and Cons of each approach** 1. **Inline**: * Pros: Reduced overhead due to fewer function calls and no need for scope creation. * Cons: Increased code size, potential impact on cache locality, and difficulty in maintaining separate scopes for different variables. 2. **IIFE**: * Pros: Encapsulates the code within its own scope, reducing interference with outer scopes, and allows for easier management of side effects. * Cons: Creates a new scope, which can lead to increased overhead due to the creation and garbage collection of the new scope. 3. **External function**: * Pros: Separates the code from the loop, making it easier to maintain and debug. Also, reduces the impact of scope creation and garbage collection. * Cons: Requires an additional function declaration, which can increase code size. **Library usage** None of the provided benchmarks explicitly use any external libraries or frameworks that would affect their performance. **Special JS features** No special JavaScript features are used in this benchmark. The focus is solely on the three approaches to measuring performance differences. **Alternatives** Other alternatives for benchmarking and measuring performance differences include: * **Function call overhead only**: Measure the performance difference between a function call with no arguments versus one with an argument. * **Closure optimization techniques**: Explore different closure optimization techniques, such as tail call optimization or recursive function calls with memoization. * **Parallel execution**: Run multiple instances of each benchmark in parallel to measure speedup benefits of multi-threading or concurrent processing. These alternatives can provide a more comprehensive understanding of performance differences and potential optimizations for specific use cases.
Related benchmarks:
Array creation with Array.reduce vs for loop vs Array.forEach(2)
JavaScript spread operator vs Slice/Splice performance - 2
JavaScript spread operator vs Slice/Splice performance, passing
JavaScript spread operator vs Slice/Splice performance 2edas
Bitwise Big array comparision 2
Comments
Confirm delete:
Do you really want to delete benchmark?