Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash partition vs forEach with array push
(version: 0)
Comparing performance of:
Lodash partition vs forEach
Created:
7 years ago
by:
Registered User
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)}})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash partition
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash partition
14986.1 Ops/sec
forEach
43011.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is crucial for optimizing code and ensuring efficient execution. The provided JSON represents a benchmark test on MeasuringThat.net, comparing two approaches: using the `lodash.partition()` function versus using a custom implementation with a `forEach` loop. **What is being tested?** The test measures the performance of two methods: 1. **Lodash partition**: Using the `lodash.partition()` function to divide an array into two parts based on a condition. 2. **Custom forEach implementation**: Creating an array `a` and another array `b`, then using a `forEach` loop to iterate over the input array `tabl`. If an item is even, it's pushed to `a`; otherwise, it's pushed to `b`. **Options compared** The two options being compared are: 1. **Lodash partition**: Using the `lodash.partition()` function, which takes an array and a predicate function as arguments. 2. **Custom forEach implementation**: Creating an array-based solution using a `forEach` loop. **Pros and Cons of each approach:** 1. **Lodash partition**: * Pros: + High-level abstraction, making the code more readable and maintainable. + Optimized for performance by using internal loop optimization techniques. * Cons: + External dependency on the Lodash library, which may increase bundle size or have security implications. + May not be suitable for very small arrays due to overhead of function calls. 2. **Custom forEach implementation**: * Pros: + No external dependencies, reducing bundle size and potential security risks. + Suitable for very small arrays without significant performance impact. * Cons: + Requires more code maintenance and readability efforts, as it's a low-level implementation. **Library: Lodash** Lodash is a popular JavaScript library providing various utility functions. The `lodash.partition()` function takes two arguments: 1. An array (`tabl` in this case). 2. A predicate function (`v => v%2`) that determines the partitioning logic. The `partition()` function returns an array containing two partitions: elements for which the predicate is true (in this case, even numbers) and elements for which the predicate is false (odd numbers). **Special JS feature or syntax** There's no specific JavaScript feature or syntax mentioned in this benchmark. The focus is on comparing the performance of two different implementation approaches. **Other alternatives** For larger-scale array processing tasks, you might consider alternative algorithms or libraries that provide optimized solutions, such as: 1. `Array.prototype.filter()` and `Array.prototype.reduce()`, which can be used together to achieve similar results. 2. `lodash.eachRight()` and `lodash.map` for iterating over arrays in reverse order or transforming elements. 3. Custom implementations using specialized data structures like Segment Trees or Suffix Arrays. When choosing an approach, consider factors such as performance requirements, code maintainability, and the size of your input array.
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?