Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash partition vs Reduce vs 2 Filter
(version: 0)
Comparing performance of:
Lodash vs Reduce vs 2 Filter
Created:
4 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
const [a, b] = _.partition(tabl, e => e === 900); const newArr = [...a,...b];
Reduce
const newArr = tabl.reduce((prevArr, currVal) => {if(currVal.key === 900) {return [currVal, ...prevArr]} else {return [...prevArr, currVal]}}, [])
2 Filter
const a = tabl.filter(e => e === 900); const b = tabl.filter(e => e !== 900); const newArr = [...a,...b];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash
Reduce
2 Filter
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark compares three approaches to partition an array of 10,000 elements: Lodash's `partition`, Reduce, and two Filter methods. The goal is to determine which approach is the most efficient. **Lodash Partition** The first test case uses Lodash's `partition` method, which splits an array into two parts based on a predicate function (`e => e === 900`). In this specific benchmark, the predicate function checks if each element in the array is equal to 900. Pros: * Simplistic and straightforward implementation * Utilizes Lodash's optimized algorithms for performance Cons: * Requires including Lodash library in the test environment * Limited control over the partitioning process **Reduce** The second test case uses the `reduce` method, which applies a reduction function to each element in the array. In this case, the reduction function checks if the current element's `key` property is equal to 900 and returns an array with the matching element or a new array of matching elements. Pros: * Utilizes JavaScript's built-in `reduce` method for performance * More control over the partitioning process Cons: * Can be slower than Lodash's optimized implementation * Requires more code and logic for implementation **2 Filter** The third test case uses two separate `filter` methods to create two arrays: one with elements equal to 900 and another with elements not equal to 900. The resulting arrays are then concatenated using the spread operator (`[...a,...b]`). Pros: * Utilizes JavaScript's built-in `filter` method for performance * Simple and straightforward implementation Cons: * Requires two separate filter operations, which can be slower than a single partitioning operation * More code and logic is required for implementation **Library and Special Features** In this benchmark, Lodash's library is used in the first test case. The `partition` method is a utility function that splits an array into two parts based on a predicate function. No special JavaScript features or syntax are mentioned in the benchmark, so we won't dive into those topics. **Alternative Approaches** Other approaches to partitioning arrays include: * Using `Array.prototype.filter()` and `Array.prototype.push()` * Utilizing a custom implementation using bitwise operations (e.g., `Array.prototype.indexOf()` and `Array.prototype splice()`) * Leveraging libraries like Ramda or Fastify's built-in partition function Each of these approaches has its own pros and cons, and the choice of which one to use depends on the specific requirements and constraints of the project. **Benchmark Considerations** When interpreting benchmark results, consider factors such as: * Number of executions per second (e.g., `ExecutionsPerSecond`) * Browser and device platform * Operating system * Code optimization techniques used Keep in mind that benchmarking can be complex, and results may vary depending on the specific environment and context.
Related benchmarks:
lodash partition vs forEach with array push vs two filter loops
lodash partition vs reduce with array push vs two filter loops
Unshift value in array - Lodash partition vs Reduce vs 2 Filter
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?