Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Loop (partition)
(version: 9)
Comparing performance of:
Reduce vs Regular loop vs Of loop vs Of loop (let not const)
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
arr = [...Array(50000)].map((v, i) => (i + 1)) filter = (el) => el % 2 === 0
Tests:
Reduce
arr.reduce( ([pass, fail], elem) => { if (filter(elem)) { return [[...pass, elem], fail]; } else { return [pass, [...fail, elem]]; } }, [[], []], );
Regular loop
const pass = []; const fail = []; for (let i = 0; i < arr.length; i++) { if (filter(arr[i])) { pass.push(arr[i]); } else { fail.push(arr[i]); } }
Of loop
const pass = []; const fail = []; for (const item of arr) { if (filter(item)) { pass.push(item); } else { fail.push(item); } }
Of loop (let not const)
const pass = []; const fail = []; for (let item of arr) { if (filter(item)) { pass.push(item); } else { fail.push(item); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Reduce
Regular loop
Of loop
Of loop (let not const)
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'd be happy to help explain the provided benchmark. **What is being tested?** The benchmark tests three different approaches for partitioning an array in JavaScript: 1. **Reduce**: Using the `reduce` method with a callback function to iterate over the array and collect elements into two separate arrays (`pass` and `fail`) based on whether they pass or fail a filter condition. 2. **Regular loop**: Using a traditional `for` loop to iterate over the array and collect elements into two separate arrays (`pass` and `fail`) based on whether they pass or fail a filter condition. 3. **Of loop**: Using an optional chaining (`of`) loop (introduced in ECMAScript 2020) to iterate over the array and collect elements into two separate arrays (`pass` and `fail`) based on whether they pass or fail a filter condition. **Options compared** The benchmark compares three different approaches for partitioning an array: * **Reduce**: Using the `reduce` method, which is a built-in array method that reduces the array to a single value. * **Regular loop**: Using a traditional `for` loop with index iteration. * **Of loop**: Using an optional chaining (`of`) loop, which is a new type of loop introduced in ECMAScript 2020. **Pros and cons** Here's a brief summary of the pros and cons of each approach: * **Reduce**: + Pros: concise, efficient, and readable. + Cons: may not be as intuitive for complex filtering logic. * **Regular loop**: + Pros: flexible, allows for custom filtering logic, and can be easier to read for some use cases. + Cons: more verbose, slower, and less concise than `reduce`. * **Of loop**: + Pros: concise, readable, and efficient. (Note that the "efficiency" benefit is likely due to the compiler's ability to optimize this type of loop.) + Cons: may not be supported by older browsers or engines. **Library/Features** None of the benchmark tests use external libraries or special JavaScript features beyond what's built into the standard library. **Special JS features** The only notable feature used in this benchmark is the optional chaining (`of`) loop, which was introduced in ECMAScript 2020. This type of loop allows for concise and expressive iteration over arrays without the need for explicit indexing or looping constructs. Overall, this benchmark provides a useful comparison between three different approaches for partitioning an array in JavaScript, highlighting the trade-offs between conciseness, readability, and performance.
Related benchmarks:
Array iteration_0001
filter-map vs reduce vs reduce with destructuring
Reduce vs Loop (partition) 1000
Flat map + filter vs. Reduce
Comments
Confirm delete:
Do you really want to delete benchmark?