Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for loop vs forEach vs while
(version: 0)
Comparing performance of:
for loop vs forEach vs while
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
for loop
for (let i = 0; i < arr.length; i++) { console.log(arr[i]) }
forEach
arr.forEach(console.log)
while
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
for loop
forEach
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
2.2 Ops/sec
forEach
2.0 Ops/sec
while
2.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark compares the performance of three different approaches to iterate over an array: `for` loop, `forEach`, and `while` loop. **Script Preparation Code** The script preparation code is identical for all three test cases: ```javascript var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++; ``` This code creates an array `arr` with a length of 100,000 and initializes its elements with consecutive integers from 0 to 99,999. **Benchmark Definition** The benchmark definition is an object that specifies the test cases: ```json [ { "Benchmark Definition": "for (let i = 0; i < arr.length; i++) {\r\n console.log(arr[i])\r\n}", "Test Name": "for loop" }, { "Benchmark Definition": "arr.forEach(console.log)", "Test Name": "forEach" }, { "Benchmark Definition": "let i = 0\r\n\r\nwhile (i < arr.length) {\r\n console.log(arr[i])\r\n i++\r\n}", "Test Name": "while" } ] ``` The three test cases are: 1. `for` loop 2. `forEach` 3. `while` loop **Options Compared** The benchmark compares the performance of each approach to iterate over the array. **Pros and Cons of Each Approach** 1. **For Loop** * Pros: + Can be used with arrays and objects. + Allows for more control over the iteration process (e.g., incrementing an index variable). * Cons: + May require manual indexing, which can lead to errors. + May not be as efficient as other approaches for very large datasets. 2. **ForEach** * Pros: + Simplifies iteration and reduces boilerplate code. + More concise than traditional `for` loops. * Cons: + Can be slower due to the overhead of function calls. + Limited control over the iteration process. 3. **While Loop** * Pros: + Provides more control over the iteration process (e.g., incrementing an index variable). + May be faster than `for` loops for very large datasets. * Cons: + Requires manual indexing, which can lead to errors. + Can be less readable than other approaches. **Library Used** None of the test cases use a library specifically. However, it's worth noting that some browsers and JavaScript engines may provide additional optimizations or features for arrays and loops (e.g., `for...of` loops). **Special JS Features or Syntax** None of the test cases explicitly use any special JavaScript features or syntax. **Other Considerations** * Performance: The benchmark measures the execution speed of each approach. However, other factors like memory usage, cache hits, and branch prediction may also impact performance. * Readability: While readability is not explicitly measured in this benchmark, it's essential to consider code maintainability, conciseness, and understandability when choosing an iteration approach. **Alternatives** For large datasets or performance-critical applications: * `for...of` loops (in modern browsers and JavaScript engines) * `map()`, `filter()`, and other array methods optimized for parallel processing * Iterators and generators For more complex data structures, such as objects with nested arrays or matrices: * Recursive functions or specialized libraries for data manipulation * WebAssembly or native code optimizations for performance-critical applications
Related benchmarks:
Array loop vs for of loop vs foreach vs map
index loop vs for-of loop vs foreach vs map
Array loop vs for of loop vs foreach vs map (2)
Array loop vs foreach vs map (Small arrays)
Reading array length inside vs outside for loop
Comments
Confirm delete:
Do you really want to delete benchmark?