Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reducer function performance
(version: 0)
Testing ops/sec between creating a new array w/ spread initializer vs accumulator.push() in array.reduce()
Comparing performance of:
New array with spread vs Accumulator push
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
New array with spread
const testData = [...Array(100000).keys()]; const evens = testData.reduce((acc, v) => { if(v % 2 === 0) { acc = [...acc, v]; } return acc; }, []); console.log(evens.length);
Accumulator push
const testData = [...Array(100000).keys()]; const evens = testData.reduce((acc, v) => { if(v % 2 === 0) { acc.push(v); } return acc; }, []); console.log(evens.length);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
New array with spread
Accumulator push
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.1:latest
, generated one year ago):
Let's dive into the details of this benchmark test case. **Benchmark Description** The benchmark tests the performance of two different approaches to creating an array of even numbers from a large dataset (100,000 elements) using JavaScript's `reduce()` method. **Test Cases** There are two individual test cases: 1. **"New array with spread"**: This test uses the spread operator (`[...Array(100000).keys()];`) to create a new array of 100,000 elements and then uses `reduce()` with an accumulator function that creates a new array of even numbers using `[...acc, v]`. 2. **"Accumulator push"**: This test also uses the spread operator (`[...Array(100000).keys()];`) to create a new array of 100,000 elements and then uses `reduce()` with an accumulator function that pushes even numbers onto an existing array using `acc.push(v)`. **Options Compared** The two test cases compare the performance of: 1. **Creating a new array vs modifying an existing array**: In the "New array with spread" test case, a new array is created from scratch using the spread operator, whereas in the "Accumulator push" test case, the accumulator function modifies an existing empty array. 2. **Using `[...acc, v]` vs `acc.push(v)`**: Both tests use `reduce()` with an accumulator function that filters even numbers, but they differ in how the filtered values are added to the accumulator: one uses a spread operator (`[...acc, v]`) and the other uses the `push()` method (`acc.push(v)`). **Pros/Cons of Different Approaches** 1. **Creating a new array vs modifying an existing array**: Creating a new array from scratch can be faster than modifying an existing array because it avoids potential performance issues with array modifications (e.g., shifting elements when pushing or popping). 2. **Using `[...acc, v]` vs `acc.push(v)`**: The spread operator (`[...acc, v]`) creates a new array and copies all existing elements to the new array, which can be slower than simply pushing an element onto the existing array using `push()`. However, when working with very large arrays or when performance is critical, this approach may be more efficient. **Other Considerations** 1. **Browser/Engine Optimization**: Some JavaScript engines (e.g., V8 in Chrome) may optimize array operations like `reduce()` and `push()`, which could affect the benchmark results. 2. **Test Data Size**: The test case uses a large dataset of 100,000 elements, which is representative of real-world scenarios where performance matters. **Library/Feature Used** None specific libraries or features are used in this benchmark test case. **Alternatives** If you need to create an array of even numbers from a large dataset, consider using: 1. **Array filtering**: Use the `filter()` method to create an array of even numbers directly without modifying an existing array. 2. **Array map**: Use the `map()` method to transform each element into an object or a value that can be easily filtered (e.g., creating an object with a flag indicating whether the number is even). Please note that this explanation focuses on the provided JSON data and the benchmark test case, so some aspects might not be exhaustive.
Related benchmarks:
Spread vs mutating
reducer function performance v2
reduce() push vs reduce() spread
flatMap vs reduce spread vs reduce push
Comments
Confirm delete:
Do you really want to delete benchmark?