Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash partition vs reduce with array push vs two filter loops
(version: 0)
Comparing performance of:
Lodash partition vs reduce with array push vs double filter
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var tabl = Array.from({ length: 10000 }).map((value, i) => i)
Tests:
Lodash partition
const [a, b] = _.partition(tabl, e => e % 2)
reduce with array push
const partition = (a, p) => a.reduce((result, e, i) => (result[p(e) ? 0 : 1].push(e), result), [[], []]); const predicate = e => e % 2; const [a, b] = partition(tabl, predicate);
double filter
const a = tabl.filter(e => e % 2); const b = tabl.filter(e => !e % 2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash partition
reduce with array push
double filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash partition
24559.6 Ops/sec
reduce with array push
18855.5 Ops/sec
double filter
16049.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net JavaScript microbenchmarks. **Benchmark Definition** The benchmark is designed to compare three approaches for partitioning an array: 1. Lodash `partition` function 2. Reducing an array with a push accumulator 3. Two separate filter loops These approaches are compared to determine which one is the most efficient. **Options Compared** Here's a brief overview of each option: * **Lodash `partition` function**: This method takes two arguments: the input array and a predicate function that determines whether an element belongs in one group (e.g., even indices) or another (e.g., odd indices). The function returns an array with two subarrays, where elements are grouped according to their index value. * **Reduce with array push**: This approach uses a `reduce` method to iterate over the input array and accumulate results. It iterates over the array, pushing elements into separate arrays based on whether they meet the predicate condition. * **Two separate filter loops**: This option involves two separate filter functions that are chained together. The first filter loop checks if an element is even (i.e., meets the predicate condition) and pushes it to one array; the second loop filters out odd indices. **Pros and Cons** Here's a brief summary of each approach: * **Lodash `partition` function**: + Pros: Efficient, concise, and well-tested. + Cons: May not be familiar to developers without Lodash experience. * **Reduce with array push**: + Pros: Can be more flexible and adaptable than filter loops. + Cons: May require more boilerplate code and is less straightforward for beginners. * **Two separate filter loops**: + Pros: Straightforward, easy to understand, and no dependencies on Lodash or other libraries. + Cons: Less efficient due to the overhead of filtering and pushing elements. **Library: Lodash** Lodash is a popular JavaScript library that provides utility functions for tasks like array manipulation. The `partition` function is one such utility that helps you separate an array into two groups based on a predicate condition. In this benchmark, Lodash is used as a reference implementation to compare the performance of alternative approaches. **Special JS Feature: Spread Operator** The spread operator (`...`) is used in the `Script Preparation Code` to create a new array with 10,000 elements. The array is created using an arrow function that maps over an object with a length property set to 10,000. This creates an array of numbers from 0 to 9,999. **Benchmark Results** The latest benchmark results show the executions per second for each test case: * **Double filter**: 20049.39 * **Lodash partition**: 18858.46 * **Reduce with array push**: 18559.94 These results indicate that the double filter approach is the fastest, followed by Lodash `partition` and then reduce with array push. **Alternatives** If you're looking for alternative approaches or ways to optimize your own code, consider: 1. Using a more efficient data structure, like a binary search tree. 2. Leveraging modern JavaScript features, such as iterators and generators. 3. Investigating other libraries or frameworks that offer similar functionality to Lodash. Keep in mind that the best approach will depend on the specific requirements of your project and your personal coding style.
Related benchmarks:
lodash partition vs forEach with array push vs two filter loops
lodash partition vs forEach with array spread iterator vs array push
lodash partition vs forEach with array spread iterator vs array push vs for each with concat vs two filters
lodash partition vs forEach with array spread iterator vs array push vs for each with concat vs two filters with 10000
Comments
Confirm delete:
Do you really want to delete benchmark?