Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs map vs for..of
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs some vs for..of
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(1000).fill(1);
Tests:
for
const result = []; for (var i = 0; i < array.length; i++) { result.push(array[i] * 2); }
foreach
const result = []; array.forEach(function(i) { result.push(i * 2); });
some
const result = array.map(function(i) { return i * 2; });
for..of
const result = []; for (var i of array) { result.push(i * 2); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
some
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
28 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
245235.0 Ops/sec
foreach
132059.3 Ops/sec
some
147260.9 Ops/sec
for..of
122913.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided benchmark compares the performance of four different loop constructs in JavaScript: `for`, `foreach` (also known as `forEach`), `map`, and `for...of`. The test case uses an array with 1000 elements to measure the execution time of each loop construct. **Options compared** * `for`: A traditional loop construct that increments a counter variable (`i`) until it reaches the end of the array. * `foreach`: A loop construct that iterates over an array using its iterator protocol, allowing for more concise code and easier maintenance. The callback function is executed for each element in the array. * `map`: A method that creates a new array with the results of applying a provided function to every element in this array. This approach is often used when you need to transform data without modifying the original array. * `for...of`: A newer loop construct introduced in ECMAScript 2015 (ES6) that allows for more concise and expressive iteration over arrays and other iterables. **Pros and Cons of each approach** 1. **`for`**: * Pros: Easy to understand, widely supported, and works well with most operations. * Cons: Can lead to performance issues if the loop counter is not properly optimized (e.g., using `i++` instead of `i += 1`). 2. **`foreach`**: * Pros: Concise code, easy to maintain, and suitable for iterating over arrays or objects. * Cons: May require additional setup for handling edge cases (e.g., empty arrays) or caching intermediate results. 3. **`map`**: * Pros: Transformative power, efficient, and works well with data pipelines. * Cons: Creates a new array, which can lead to memory overhead, especially for large datasets. 4. **`for...of`**: * Pros: Concise code, readable, and suitable for modern JavaScript environments. * Cons: Less widely supported in older browsers (e.g., IE 11), but still works in most modern browsers. **Library usage** None of the benchmark test cases use external libraries. However, if a library were used, it might be something like `lodash` or `moment.js`, which are commonly used for utility functions and date manipulation. **Special JS features or syntax** The only special feature used is `for...of`, which was introduced in ECMAScript 2015 (ES6). It's a newer loop construct that allows for more concise and expressive iteration over arrays and other iterables. **Other alternatives** If you want to test additional loop constructs, you could consider: * `while`: A traditional loop construct that uses a conditional statement to determine the number of iterations. * `do-while` loops: Similar to `while`, but executes the code block at least once before checking the condition. * `forEachArray`: A non-standard function for iterating over arrays (not widely supported). However, these alternatives are less common and may not be as efficient or readable as the four loop constructs tested in this benchmark.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2
map vs forEach Chris v2b
Array fill map, vs for i loop
Array fill map, vs while loop
Comments
Confirm delete:
Do you really want to delete benchmark?