Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach(with and without arrow) vs map
(version: 0)
Comparing performance of:
foreach vs for vs map vs foreach with arrow as well
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))
foreach with arrow as well
arr.forEach(item => someFn(item))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach
for
map
foreach with arrow as well
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
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of three different approaches to iterate over an array in JavaScript: 1. **Array Loop**: A traditional for loop that manually iterates over the array using an index variable. 2. **forEach**: The `forEach` method, which is a built-in array method that calls a provided function once for each element in the array. 3. **map**: The `map` method, which returns a new array with the results of applying a provided function to each element in the original array. **Options Compared** The benchmark compares the performance of these three approaches: * **Array Loop**: A traditional for loop that manually iterates over the array using an index variable. * **forEach**: The `forEach` method, which is a built-in array method that calls a provided function once for each element in the array. There are two variations: + **foreach without arrow function**: A traditional for loop with a callback function (not using arrow functions). + **foreach with arrow function**: Using an arrow function as the callback function. * **map**: The `map` method, which returns a new array with the results of applying a provided function to each element in the original array. **Pros and Cons** Here are some pros and cons of each approach: * **Array Loop**: + Pros: Direct access to elements, no overhead from built-in methods. + Cons: Requires manual index management, more prone to errors. * **forEach** (with arrow function): + Pros: Concise code, easy to read, less prone to errors compared to traditional for loops. + Cons: May have some performance overhead due to the added layer of indirection. * **map**: + Pros: Returns a new array with transformed elements, easy to chain methods together. + Cons: May have performance overhead due to creating a new array. **Library and Special JS Features** There is no library used in this benchmark. However, it does use the following special JavaScript features: * **Arrow functions**: Used in the "foreach with arrow as well" variation of the `forEach` approach. * **Built-in methods**: The `forEach` and `map` methods are built-in to the JavaScript language. **Other Alternatives** Other alternatives to iterate over arrays in JavaScript include: * Using a `for...of` loop (introduced in ECMAScript 2015) * Using `Array.prototype.reduce()` * Using `Array.prototype.every()` or `Array.prototype.some()` Note that each of these alternatives has its own trade-offs and use cases, and may not always be the best choice for every situation.
Related benchmarks:
Reverse array loop vs foreach vs map
Array loop vs foreach vs map (Small arrays)
Array loop vs foreach vs map -2
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?