Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash partion vs 2 Filter
(version: 0)
Comparing performance of:
Reduce vs Filters
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var tabl = Array.from({ length: 10000 }).map((value, i) => i)
Tests:
Reduce
const newArr = tabl.reduce((prevArr, currVal) => {if(currVal === 900) {return [currVal, ...prevArr]} else {return [...prevArr, currVal]}}, [])
Filters
const newArr = [...tabl.filter(e => e === 900),...tabl.filter(e => e !== 900)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
Filters
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.1:latest
, generated one year ago):
Let's break down the benchmark definition and test cases. **Benchmark Definition** The benchmark is titled "Lodash partition vs 2 Filter". This suggests that we're comparing three approaches to partitioning an array: two filter-based methods and Lodash's `partition` function (which, unfortunately, isn't included in this JSON). **Script Preparation Code** The script preparation code creates a large array of numbers from 0 to 9999 using `Array.from` and `map`. **Individual Test Cases** There are two test cases: 1. **Reduce**: This method uses the `reduce` function to partition the array. The benchmark definition is: ```javascript const newArr = tabl.reduce((prevArr, currVal) => {if(currVal === 900) {return [currVal, ...prevArr]} else {return [...prevArr, currVal]}}, []); ``` In this approach, we're using `reduce` to iterate over the array and create a new array by pushing values into it. If a value is equal to 900, we add it to the beginning of the new array. 2. **Filters**: This method uses two filters to partition the array. The benchmark definition is: ```javascript const newArr = [...tabl.filter(e => e === 900),...tabl.filter(e => e !== 900)]; ``` In this approach, we're using two separate filters to extract values equal to 900 and not equal to 900, respectively. We then concatenate these two arrays. **Latest Benchmark Result** The benchmark results show the execution time per second for each test case on a Chrome browser running on Mac OS X 10.15.7: * **Filters**: 7684.25 executions per second * **Reduce**: 13.63292121887207 executions per second **Analysis** In this benchmark, we're comparing two different approaches to partitioning an array: `reduce` and filters. The results show that the filters approach is significantly faster than the reduce approach. Pros of the filters approach: * **Readability**: This code is more concise and easier to understand. * **Performance**: As shown in the benchmark results, this approach is much faster. Cons of the filters approach: * **Memory usage**: Creating two separate arrays can be memory-intensive if the original array is large. The reduce approach has its own pros and cons. It's a good example of using functional programming techniques to achieve specific tasks, but it may not always be the most efficient solution. **Other Alternatives** There are other ways to partition an array in JavaScript. Some alternatives include: * **Array.prototype.partition()**: While this isn't included in the benchmark JSON, it's worth mentioning as a built-in method that can achieve similar results. * **lodash.partitions()**: This Lodash function is designed for partitioning arrays and might be more efficient than the reduce or filters approaches. * **Other libraries**: There are other JavaScript libraries, such as Ramda or Immutable.js, that provide their own implementation of array partitioning. Keep in mind that the best approach depends on your specific use case and requirements.
Related benchmarks:
lodash partition vs forEach with array push vs two filter loops
lodash partition vs find and filter
lodash partition vs forEach with array push vs two "some" filter loops
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?