Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach (static & index)
(version: 0)
Comparing performance of:
foreach_static vs for_static vs foreach_index vs for_index
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 1000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
foreach_static
arr.forEach(function (item){ someFn(3); })
for_static
for (var i = 0, len = arr.length; i < len; i++) { someFn(3); }
foreach_index
arr.forEach(function (item){ someFn(item); })
for_index
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach_static
for_static
foreach_index
for_index
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach_static
121221.0 Ops/sec
for_static
1188782.6 Ops/sec
foreach_index
120196.6 Ops/sec
for_index
612025.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the provided benchmark. **Benchmark Overview** The benchmark measures the performance difference between three approaches to iterate over an array: 1. Using `forEach` with a callback function that always calls `someFn(3)`. 2. Using a traditional `for` loop with an index variable. 3. Using `forEach` with a callback function that passes each element of the array to `someFn`. **Options Compared** The three options compared are: * **`foreach_static`**: Iterating over the array using `forEach` with a static callback function that always calls `someFn(3)`. + Pros: Easy to implement, concise code. + Cons: May incur overhead due to the fixed callback function. * **`for_static`**: Using a traditional `for` loop with an index variable and calling `someFn(3)` in each iteration. + Pros: More control over the iteration process, no overhead from callback functions. + Cons: Requires manual increment of the index variable and can be error-prone. * **`foreach_index`**: Iterating over the array using `forEach` with a dynamic callback function that passes each element to `someFn`. + Pros: Similar to `foreach_static`, but with more flexibility in the callback function. + Cons: May incur additional overhead due to the dynamic callback function. **Library Used** In this benchmark, no external libraries are used. The code only relies on built-in JavaScript features. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax explicitly mentioned in the benchmark. However, `forEach` and traditional `for` loops are both supported by modern JavaScript engines, making them a good choice for this comparison. **Other Considerations** When writing performance-critical code, it's essential to consider the following factors: * Cache locality: Modern CPU architectures use cache hierarchies to improve performance. Iterators that access elements in a contiguous block of memory are more likely to benefit from caching. * Branch prediction: The number of branches (conditional statements) can significantly impact performance. Optimal branching reduces the likelihood of mispredicted branches, which can slow down the execution. * Register usage: Modern CPUs use registers to store data temporarily during computations. Efficient register allocation and reuse can improve performance. **Alternatives** Other alternatives for iterating over arrays include: * `map()`: Returns a new array with the results of applying a given function to each element in the original array. * `filter()`: Returns a new array with elements that pass a test implemented by a provided function. * Array methods like `every()`, `some()`, and `reduce()` can also be used, but they may incur additional overhead due to their more complex logic. In conclusion, this benchmark helps illustrate the trade-offs between using `forEach` or traditional `for` loops for array iteration. By understanding these options and considering factors like cache locality, branch prediction, and register usage, developers can optimize their code for better performance.
Related benchmarks:
Array loop vs foreach
Array loop vs for of loop vs foreach vs map (2)
Array loop vs foreach vs map (Small arrays)
Array loop vs foreach vs map with large array
Array loop vs foreach vs for_of
Comments
Confirm delete:
Do you really want to delete benchmark?