Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS For vs reduce
(version: 0)
Comparing performance of:
For + push vs For + concat vs Reduce
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var l = 100; var a = Array(l).fill('').map(()=>Math.round(Math.random(0,1)*l)) var b = Array(l).fill('').map(()=>Math.round(Math.random(0,1)*l))
Tests:
For + push
const p = [] for (let i=0; i < l; i++){ const c = a[i] < b[i] ? [a[i], b[i]] : [b[i], a[i]]; if(!p.includes(c)){ p.push(c); } }
For + concat
let p = [] for (let i=0; i < l; i++){ const c = a[i] < b[i] ? [a[i], b[i]] : [b[i], a[i]]; if(!p.includes(c)){ p = [...p, c] } }
Reduce
const p = a.reduce((acc, v, i) => { const c = v< b[i] ? [v, b[i]]: [b[i], v]; if(!acc.includes(c)){ acc.push(c) } return acc }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For + push
For + concat
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):
I'll break down the provided benchmark and explain what's being tested, compared, and discussed. **Benchmark Overview** The benchmark is designed to compare three approaches for creating an array of unique pairs: `for + push`, `for + concat`, and `reduce`. The test case generates two arrays `a` and `b` with random elements between 0 and 100. The goal is to create a third array `p` containing only the unique pairs from `a` and `b`. **Approaches Compared** 1. **For + push**: This approach uses a traditional `for` loop to iterate over the arrays, creating a new pair on each iteration and pushing it onto the `p` array if it doesn't already exist. 2. **For + concat**: Similar to the previous approach, but instead of using `push`, it uses the `concat` method to append the new pair to the `p` array. 3. **Reduce**: This approach uses the `reduce` method to accumulate the unique pairs in the `p` array. **Pros and Cons** * **For + push**: * Pros: Easy to understand, straightforward implementation. * Cons: May lead to unnecessary allocations and copies of the arrays, especially for large datasets. * **For + concat**: * Pros: Avoids unnecessary allocations and copies like `push`, but may still lead to performance issues due to the overhead of concatenating arrays. * **Reduce**: * Pros: Efficient, as it avoids allocations and copies by using a single array for accumulation. Also, optimized for large datasets. * Cons: May require additional understanding of the `reduce` method and its behavior. **Library and Special Features** None are explicitly mentioned in this benchmark definition. **Alternatives** Other approaches to create unique pairs could include: * **Set**: Using a Set data structure (e.g., with the `Set` class in modern JavaScript) to store the unique pairs. This would be similar to the `reduce` approach but might have additional overhead due to set operations. * **Map**: Using a Map data structure (e.g., with the `Map` class in modern JavaScript) to store the unique pairs, which could offer better performance than Sets. Keep in mind that these alternatives might not necessarily outperform the existing approaches in this specific benchmark but can be explored for different use cases.
Related benchmarks:
forEach vs reduce
flatMap + reduce vs reduce + reduce
Math.max vs Array.reduce
Math.min vs reduce
Comments
Confirm delete:
Do you really want to delete benchmark?