Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce: concat vs push
(version: 0)
Comparing performance of:
Reduce + concat vs Reduce + push
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
a = new Array(1000).fill(0).map(() => ({ value: Math.random(), keep: Math.random() > 0.5 }));
Tests:
Reduce + concat
a.reduce((acc, { value, keep }) => { if (keep) { acc.push(value); } return acc; }, []);
Reduce + push
a.reduce((acc, { value, keep }) => (keep ? acc.concat([value]) : acc), []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce + concat
Reduce + push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce + concat
154008.3 Ops/sec
Reduce + push
7193.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for filtering an array of objects in JavaScript: using `push` with `concat` or using the `reduce()` method with a conditional statement. **Script Preparation Code** The script preparation code creates an array `a` with 1000 elements, each element is an object with two properties: `value` and `keep`. The `keep` property is randomly generated as either true or false. This setup ensures that about half of the objects will be kept in the final filtered array. **Html Preparation Code** There is no HTML preparation code provided, which means this benchmark only tests the JavaScript performance aspect. **Test Cases** The benchmark defines two test cases: 1. **Reduce + concat**: This approach uses `push` with `concat` to add elements to the accumulator (`acc`) and then concatenates an array of values to be added. 2. **Reduce + push**: This approach uses a similar approach, but instead of using `concat`, it pushes elements directly onto the accumulator. **Library Usage** The benchmark does not explicitly mention any libraries being used beyond the built-in JavaScript `Array` methods. **Special JS Feature or Syntax** Neither test case utilizes any special JavaScript features or syntax. **Pros and Cons of Each Approach** 1. **Reduce + concat**: * Pros: This approach is more concise and easier to read, as it uses a single method call. * Cons: It may incur additional overhead due to the use of `concat()`, which can create a new array object. 2. **Reduce + push**: * Pros: This approach avoids the potential overhead of creating a new array object using `concat()`. * Cons: It requires an additional method call (`push()`), making it slightly less concise than the first approach. **Other Considerations** The benchmark only tests performance and does not consider factors like code readability, maintainability, or memory usage. Other considerations might include: * Performance impact on specific hardware configurations * Effectiveness in handling edge cases (e.g., empty arrays, arrays with duplicate values) * Readability and maintainability for developers **Alternatives** Other approaches to filter an array of objects in JavaScript could be used, such as: 1. Using `filter()` method: This is a more concise approach that filters the array directly without creating an accumulator. 2. Using arrow functions: Another alternative would be to use an arrow function with `map()` and then using `concat()` or `push()`. 3. Looping over the array manually: For smaller arrays, looping over each element individually might be faster than using array methods. These alternatives are not specifically tested in this benchmark but demonstrate other ways to achieve similar results.
Related benchmarks:
Fill array with random integers
new Array() vs Array.from() with random data
Pushing vs Filling
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?