Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs Array loop of vs foreach vs map
(version: 0)
Comparing performance of:
foreach vs for vs map vs for of
Created:
5 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]); }
map
arr.map(item => someFn(item))
for of
for (var foo of arr) { someFn(foo); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach
for
map
for of
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
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares the performance of four different approaches to iterating over an array and applying a function to each element: * **`forEach`:** This method iterates through each element in the array and executes a provided callback function for each element. * **`for` loop:** This is a traditional loop that iterates from the first to the last element of the array, accessing each element by its index. * **`map`:** This method iterates over each element in the array, applies a provided function to each element, and returns a new array with the results. * **`for...of` loop:** This is a modern loop that iterates directly over iterable objects (like arrays) without needing an explicit index. **Pros and Cons:** * **`forEach`:** Simple and readable for basic iterations where you don't need to return a new array. * Con: Can be less efficient than other methods if you need to create a new array based on the results. * **`for` loop:** Most control over iteration, allows direct access to element indices. * Con: More verbose and can be harder to read than newer approaches. * **`map`:** Efficient for creating new arrays based on existing data. * Con: Can be less readable if you're not familiar with functional programming concepts. * **`for...of` loop:** Concise and easy to read, especially for simple iterations. * Con: Not as widely supported in older JavaScript versions. **Other Considerations:** The `someFn` function is a simple multiplier that doesn't significantly impact performance. The benchmark measures the time it takes to iterate over the array and apply the function. **Alternatives:** While these are common methods for iterating over arrays, there are other options: * **Libraries:** Libraries like Lodash or Ramda provide optimized iteration functions with more features (e.g., filtering, transforming data). * **Async Iteration:** If your array is large or contains asynchronous operations, you can use async/await or promises to iterate efficiently. Let me know if you have any other questions!
Related benchmarks:
Array loop vs for of loop vs foreach vs map (2)
Array loop vs foreach vs map (Small arrays)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Array loop vs for of loop vs foreach vs map fixed
Comments
Confirm delete:
Do you really want to delete benchmark?