Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs for vs while
(version: 0)
Test array forEach, for, while
Comparing performance of:
forEach vs for vs while
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
forEach
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; arr.forEach(el => console.log(el));
for
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; for (let i = 0, len = arr.length; i < len; i++) { console.log(arr[i]); }
while
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let i = 0; while (i < arr.length) { console.log(arr[i]); i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
forEach
for
while
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
184.8 Ops/sec
for
199.8 Ops/sec
while
201.7 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. **Benchmark Definition** The benchmark defines three types of loops: `forEach`, `for`, and `while`. The goal is to compare the performance of these three approaches in iterating over an array. **Options Compared** We have three options being compared: 1. **`forEach`**: This method iterates over an array using a callback function that is called for each element in the array. 2. **`for`**: This loop uses a traditional `for` loop with an index variable to iterate over the array. 3. **`while`**: This loop uses a conditional statement to check if the loop should continue iterating, based on the value of an index variable. **Pros and Cons** Here are some pros and cons of each approach: * **`forEach`**: + Pros: concise and expressive code, easy to read and maintain. + Cons: can be slower due to the overhead of callback functions. * **`for`**: + Pros: fast and efficient, predictable loop behavior. + Cons: can be verbose and less readable than `forEach`. * **`while`**: + Pros: flexible and adjustable loop behavior, but requires more manual management. + Cons: can be error-prone if not managed correctly. **Library** In the benchmark code, there is no explicit library being used. However, the `forEach`, `for`, and `while` loops are standard JavaScript constructs that do not rely on external libraries. **Special JS Features or Syntax** There are no special JavaScript features or syntax being tested in this benchmark. The focus is solely on comparing the performance of different loop approaches. **Other Considerations** When choosing a loop approach, consider the following factors: * Code readability and maintainability * Performance requirements (e.g., speed, efficiency) * Development time and ease of use In general, if you need to iterate over an array frequently, `forEach` is a good choice due to its concise and expressive nature. However, if performance is critical, a traditional `for` loop or `while` loop may be more suitable. **Alternatives** If you're looking for alternative loop approaches, consider the following options: * **`reduce()`**: A method that accumulates values in an array by applying a callback function to each element. * **`map()`**: A method that creates a new array with transformed values using a callback function. * **`every()`, `some()`, and `any()`**: Methods that test conditions on elements of an array. Keep in mind that these methods may have different performance characteristics than the traditional loop approaches.
Related benchmarks:
for vs foreach vs some
for vs foreach vs some big
foreach vs for..of
For vs Foreach vs Do While vs While
foreach vs for...of
Comments
Confirm delete:
Do you really want to delete benchmark?