Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach + push vs. recude + ...
(version: 0)
Compare forEach with push against reduce with spread operator
Comparing performance of:
forEach vs reduce
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateTestArray() { const result = []; for (let i = 0; i < 1000000; ++i) { result.push({ index: i, r: Math.floor(Math.random() * 25), }); } return result; }
Tests:
forEach
const array = generateTestArray(); const unique = []; array.forEach((x) => { if (!unique.includes(x.r)) unique.push(x.r); });
reduce
const array = generateTestArray(); const unique = array.reduce((a, x) => a.includes(x.r) ? a : [...a, x.r], [] );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
reduce
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 that compares two approaches to achieve a similar result: using `Array.prototype.forEach()` with `push()` versus using `Array.prototype.reduce()` with the spread operator (`...`). **Options being compared:** 1. **`forEach + push`**: This approach uses the `forEach()` method to iterate over an array, and for each element, it pushes a new value onto another array. 2. **`reduce + ...`**: This approach uses the `reduce()` method to accumulate values in an array, starting with an initial value (in this case, an empty array). The spread operator (`...`) is used to merge the current element into the accumulator. **Pros and Cons:** 1. **`forEach + push`**: * Pros: + Easy to understand and implement. + No need to create an accumulator array. * Cons: + Can be less efficient due to the overhead of function calls and memory allocation for each element's value. + May not be as scalable for large datasets, as it requires multiple `push()` operations. 2. **`reduce + ...`**: * Pros: + Can be more efficient, especially for large datasets, since it only requires a single iteration over the array and uses a single accumulator array. + Scalable and flexible, allowing for various initial values and accumulation functions. * Cons: + May require more complex understanding of the `reduce()` method and its usage. **Library/Features:** In this benchmark, there are no specific libraries or features being tested. The focus is solely on comparing two different JavaScript methods (`forEach` and `reduce`) with their respective syntax. However, it's worth noting that some browsers may have specific optimizations or implementations for these methods, which could potentially impact the results of this benchmark. **Special JS feature/Syntax:** There are no special features or syntax being tested in this benchmark. The focus is on comparing the performance of two standard JavaScript methods. Overall, this benchmark provides a simple and controlled environment to compare the performance of two different approaches to achieving a similar result, allowing users to evaluate their own code's efficiency and choose the best approach for their specific use cases.
Related benchmarks:
Array.prototype.slice vs spread operator proper
JavaScript spread operator vs Slice/Splice performance testing
JavaScript spread operator vs Slice/Splice performance 2edas
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Comments
Confirm delete:
Do you really want to delete benchmark?