Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Partition Arrays
(version: 0)
Comparing performance of:
Reduce + Spread vs Iteration
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getRandomIntInclusive(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive } var arrayOfRandomInts = Array.from({length: 1000}, () => getRandomIntInclusive(1,10));
Tests:
Reduce + Spread
function partition(array, isValid) { return array.reduce(([pass, fail], elem) => { return isValid(elem) ? [[...pass, elem], fail] : [pass, [...fail, elem]]; }, [[], []]); } const [pass, fail] = partition(arrayOfRandomInts, (e) => e > 5); console.log(pass.length, fail.length);
Iteration
function partition(array, isValid) { const pass = []; const fail = []; array.forEach(elem => { isValid(elem) ? pass.push(elem) : fail.push(elem); }); return [pass, fail]; } const [pass, fail] = partition(arrayOfRandomInts, (e) => e > 5); console.log(pass.length, fail.length);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce + Spread
Iteration
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 break down the provided JSON and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark is defined by a JavaScript function named `partition` that takes an array and a validation function as arguments. The function returns two arrays: one for elements that pass the validation and another for elements that fail. **Test Cases** There are two test cases: 1. **Reduce + Spread**: This test case uses the `reduce` method along with the spread operator (`...`) to partition the array. 2. **Iteration**: This test case uses a traditional `forEach` loop to iterate through the array and partition it. **Library: Lodash** The `partition` function in both test cases is likely using the Lodash library, which provides a `partition` function that can be used to split an array into two parts based on a predicate. ```javascript const _ = require('lodash'); // or import { partition } from 'lodash'; ``` **Special JS Features/Syntax** There are no special JavaScript features or syntax mentioned in the benchmark definition. However, it's worth noting that the `reduce` method and spread operator (`...`) are modern JavaScript features. **Pros and Cons of Approaches** 1. **Reduce + Spread** * Pros: concise, efficient, and easy to read. * Cons: may be less intuitive for developers who are not familiar with these features. 2. **Iteration** * Pros: widely supported, no special features or syntax required. * Cons: may be less efficient than the `reduce` method. **Other Alternatives** If you want to implement a similar partitioning function without using Lodash or the `reduce` method, you can use a traditional loop: ```javascript function partition(array, isValid) { let pass = []; let fail = []; for (let elem of array) { if (isValid(elem)) { pass.push(elem); } else { fail.push(elem); } } return [pass, fail]; } ``` Alternatively, you can use a more functional programming-style approach with the `filter` method: ```javascript function partition(array, isValid) { return [array.filter(isValid), array.filter((elem) => !isValid)]; } ``` In summary, the benchmark is testing two different approaches to partitioning an array: using the `reduce` method with spread operator (`Reduce + Spread`) and a traditional loop (`Iteration`). The choice of approach depends on personal preference, familiarity with modern JavaScript features, and the desired level of efficiency.
Related benchmarks:
Fisher-Yates Shuffle
Set.has v.s Array.includes
yoooooo
Set.has v.s Array.includes v2
Comments
Confirm delete:
Do you really want to delete benchmark?