Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.forEach() vs .reduce vs for of vs for test2
(version: 0)
Comparing performance of:
array.forEach() vs array.reduce() vs for of vs for
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (var i = 0; i < 10000; i++) { array[i] = i; } var firstTestResults = []; var secondTestResults = []; var thirdTestResults = []; var fourthTestResults = [];
Tests:
array.forEach()
array.forEach((value) => { value = value + 10; if (value % 2 === 0) { firstTestResults.push(value); } });
array.reduce()
secondTestResults = array.reduce((acc, value) => { value = value + 10; if (value % 2 === 0) { acc.push(value); } return acc; }, []);
for of
for(value of array) { value = value + 10; if (value % 2 === 0) { thirdTestResults.push(value); } }
for
var value; for(let i = 0; i < array.length; i++){ value = array[i] + 10; if (value % 2 === 0) { fourthTestResults.push(value); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array.forEach()
array.reduce()
for of
for
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 dive into the world of JavaScript microbenchmarks. The provided JSON represents a benchmark test case on MeasureThat.net, which compares the performance of four different methods for iterating over an array: `forEach`, `reduce`, `for...of`, and `for`. The test case is designed to measure how much time each method takes to iterate over an array of 10,000 elements. **Options Compared** The four options being compared are: 1. **`array.forEach()`**: This method iterates over the array using a callback function that executes for each element. 2. **`array.reduce()`**: This method iterates over the array by reducing it to a single value using an accumulator. 3. **`for...of`**: This is a modern iteration method introduced in ECMAScript 2015, which allows iterating over arrays without a callback function. 4. **`for`**: This is an older iteration method that uses a traditional loop with an index variable. **Pros and Cons of Each Approach** 1. **`array.forEach()`**: * Pros: Simple to implement, flexible, and easy to read. * Cons: May be slower than other methods due to the overhead of function calls. 2. **`array.reduce()`**: * Pros: Can be used as a single pass over the array, reducing memory allocation. * Cons: May have higher overhead due to the need to compute an accumulator value. 3. **`for...of`**: * Pros: Modern, efficient, and easy to read. * Cons: Only works on arrays, not objects or other iterable types. 4. **`for`**: * Pros: Well-established, widely supported, and simple to implement. * Cons: Less flexible than `forEach()` or `for...of`, with more boilerplate code. **Library Usage** None of the options in this benchmark test case rely on any external libraries. All four methods are part of the JavaScript language itself. **Special JS Features/Syntax** The `for...of` iteration method is a special syntax introduced in ECMAScript 2015, which allows iterating over arrays without a callback function. This is not widely supported in older browsers or versions of JavaScript. **Other Considerations** When choosing an iteration method, consider the trade-offs between performance, readability, and maintainability. For small to medium-sized datasets, `forEach()` or `for` might be sufficient. For larger datasets or high-performance requirements, `reduce()` or `for...of` might be more suitable. In terms of alternatives, you could also explore other iteration methods, such as: * **`Array.prototype.every()`**: Similar to `forEach()`, but returns a boolean indicating whether all elements passed the test. * **`Array.prototype.some()`**: Similar to `forEach()`, but returns a boolean indicating whether at least one element passed the test. * **`setInterval()`** or **`setTimeout()`**: Can be used for iterating over arrays, but have limitations in terms of performance and predictability. Keep in mind that the choice of iteration method ultimately depends on the specific requirements and constraints of your project.
Related benchmarks:
foreach vs for..of
foreach vs for...of
For vs for of vs forEach
For loop vs <Array>.forEach() vs for...of loop
for vs foreach vs for..of vs for..of over entries vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?