Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash partition vs forEach with array push vs two filter loops
(version: 0)
Comparing performance of:
Lodash partition vs forEach vs double filter
Created:
6 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
let result = _.partition(tabl, v => v%2)
forEach
let a = [], b = []; let result = tabl.forEach(item => {if (item%2) {a.push(item)} else {b.push(item)}})
double filter
const a = tabl.filter(item => item%2); const b = tabl.filter(item => !item%2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash partition
forEach
double filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash partition
32760.2 Ops/sec
forEach
37765.2 Ops/sec
double filter
28637.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the Benchmark Definition json and explain what's being tested. The test is designed to compare three approaches for filtering an array in JavaScript: 1. **Lodash `partition` method**: This approach uses the `partition` function from the Lodash library, which splits the array into two parts: one containing elements that pass a certain condition (in this case, `v => v % 2 == 0`) and another containing elements that do not. 2. **`forEach` loop with array push**: This approach uses a traditional `forEach` loop to iterate over the array and push elements into two separate arrays (`a` and `b`) based on whether they meet the condition. 3. **Double filter loops**: This approach uses two separate `filter` methods to create two new arrays, one containing elements that pass the condition and another containing elements that do not. **Pros and Cons of each approach:** * Lodash `partition` method: + Pros: Efficient and concise, eliminates the need for explicit loop variables. + Cons: Relies on an external library (Lodash), may be less familiar to those without experience with functional programming. * `forEach` loop with array push: + Pros: Easy to understand and implement, no external dependencies required. + Cons: Less efficient than `partition`, can lead to slower performance due to the overhead of creating new arrays. * Double filter loops: + Pros: No external dependencies required, easy to understand and implement. + Cons: Can be less concise and more verbose than `partition` or `forEach` loop, may require additional array manipulation. **Library usage:** The Lodash library is used for the `partition` method. This library provides a set of utility functions for tasks such as array manipulation, string manipulation, and functional programming. **Special JS features or syntax:** None are mentioned in this benchmark definition. However, it's worth noting that some modern JavaScript features like async/await, classes, and modules (ES6+) may be used in the script preparation code or other test cases. **Other alternatives:** Some alternative approaches to filtering an array could include: * Using `Array.prototype.filter()` method directly on the original array. * Using a regular expression with `String.prototype.match()` and `Array.prototype.push()`. * Using a custom implementation using bitwise operations or other low-level techniques. Keep in mind that each of these alternatives may have their own trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
lodash partition vs forEach with array push
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?