Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach vs every
(version: 0)
Comparing performance of:
foreach vs for vs every
Created:
2 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
arr.forEach(function (item){ someFn(item); })
for
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); }
every
arr.every(function (item){ someFn(item); return true; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
foreach
for
every
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
6726.2 Ops/sec
for
2813.2 Ops/sec
every
7322.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking setup. **Benchmark Overview** The test aims to compare the performance of three different ways to iterate over an array in JavaScript: `forEach`, `for` loop, and `every`. The benchmark measures the number of executions per second for each approach on a specific scenario with 1000 elements. **Options Compared** 1. **`forEach"`: * This method iterates over the array using a callback function that is called for each element. * It's often used when you need to perform an operation on each element, without having direct access to the index (i.e., `item` instead of `arr[i]`). * In this benchmark, it's executed 6726.15 times per second, which is faster than the other two approaches. 2. **`for` Loop**: * This method uses a traditional loop structure to iterate over the array elements. * It provides direct access to the index (i.e., `arr[i]`) and can be more efficient for simple iteration. * However, it might be slower than `forEach` due to the overhead of managing the loop variables. * In this benchmark, it's executed 2813.23 times per second, which is slower than `forEach`. 3. **`every`**: * This method iterates over the array using a callback function that is called for each element, similar to `forEach`. * However, it returns `true` as soon as all elements pass the test (i.e., the callback function returns `true`), whereas `forEach` continues iterating until all elements have been processed. * In this benchmark, it's executed 7322.62 times per second, which is faster than both `for` loop and `forEach`. **Pros and Cons** 1. **`forEach`**: * Pros: Often easier to use for simple iteration, provides more readable code. * Cons: May be slower due to callback function overhead, less control over indexing. 2. **`for` Loop**: * Pros: Provides direct access to index, can be faster in some cases (e.g., when the number of elements is small). * Cons: More verbose and error-prone code, requires manual index management. 3. **`every`**: * Pros: Can be faster due to early return condition, provides a clear test for all elements passing a predicate function. * Cons: May not be suitable for simple iteration, returns `true` as soon as any element fails the predicate. **Libraries and Special Features** None mentioned in the benchmark code. **Alternative Approaches** Other alternatives to consider when iterating over arrays include: 1. **`map()`**: Creates a new array with the results of applying a given function on every element in this array. 2. **`reduce()`**: Applies a reducer function to all elements in an array (e.g., summing values) and returns a single output value. 3. **`filter()`**: Creates a new array with all elements that pass the test implemented by the provided function. Keep in mind that each approach has its strengths and weaknesses, and the choice of which one to use depends on the specific requirements of your project.
Related benchmarks:
Array loop vs foreach
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?