Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash partition VS native reduce (updated)
(version: 1)
Comparing performance of:
Lodash vs Native reduce
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
Script Preparation code:
var arr = []; for (i = 0; i < 1000; i++) { arr[i] = i; }
Tests:
Lodash
var [met, rest] = _.partition(arr, function(i) { return i % 2 === 0 });
Native reduce
var [met, rest] = arr.reduce(function([p1, p2], i) { return i % 2 === 0 ? [[...p1, i], p2] : [p1, [...p2, i]] }, [[], []])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Native reduce
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
418258.8 Ops/sec
Native reduce
7542.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests the performance of two different approaches to partitioning an array of integers: using the Lodash library's `partition` method and using the native JavaScript `reduce` method. The array being tested consists of 1000 integers from 0 to 999. ### Options Compared 1. **Lodash Partition** - **Benchmark Definition:** ```javascript var [met, rest] = _.partition(arr, function(i) { return i % 2 === 0 }); ``` - **Test Name:** Lodash 2. **Native Reduce** - **Benchmark Definition:** ```javascript var [met, rest] = arr.reduce(function([p1, p2], i) { return i % 2 === 0 ? [[...p1, i], p2] : [p1, [...p2, i]] }, [[], []]); ``` - **Test Name:** Native reduce ### Pros and Cons #### Lodash Partition - **Pros:** - Clean and readable syntax that abstracts the partitioning logic. - Well-optimized for performance in many cases, especially for larger datasets or complex logic. - Handles edge cases and nuances which developers might otherwise have to consider when writing custom logic. - **Cons:** - Introduces an external dependency (Lodash), which is an additional library that increases the bundle size if used in a browser. - May have overhead due to additional function calls, which can be significant for simple operations. #### Native Reduce - **Pros:** - No additional library or external dependencies, leading to smaller bundle sizes. - Can be optimized by modern JavaScript engines since it leverages built-in methods. - **Cons:** - More complex and less readable code compared to Lodash. The use of object destructuring (`[p1, p2]`) and the ternary operator may lead to decreased clarity. - May result in worse performance due to the creation of new arrays with the spread operator (`[...p1, i]` and `[...p2, i]`), which is less efficient than modifying existing arrays directly. ### Libraries and Features - **Lodash Library:** - Lodash is a popular JavaScript utility library that provides various functions for common programming tasks, including data manipulation, array handling, and more. In this benchmark, Lodash's `partition` method is used to split an array into two groups based on a predicate function. ### Considerations for Software Engineers When choosing between these approaches, developers should consider: - **Code Readability vs. Performance:** If performance is a crucial factor for a large dataset, profiling both methods in the specific context of application use is advisable. - **Library Usage and Bundle Size:** If minimizing the bundle size is important, especially for front-end applications, using native methods may be preferred. - **Ecosystem:** If the project already includes Lodash or similar utility libraries extensively, using Lodash methods may provide consistent coding patterns. ### Alternatives Other alternatives for partitioning an array in JavaScript could include: - **Using a Simple Loop:** Manually iterating over the array and pushing items into two different arrays based on the condition. - **Using Functional Programming:** Options like `filter` in conjunction with `map` could achieve similar results, although they would require additional operations. Overall, the choice of approach can depend significantly on the specific use case, performance requirements, and preferences for code maintainability.
Related benchmarks:
Lodash reduce with native reduce
Lodash partition VS native reduce
reduce native vs lodash
Lodash vs Native reduce - 2020 test
Lodash vs Native reduce on lqrgeobject
Lodash flatten & map vs native js
lodash - reduce vs forEach with large array to object
Lodash partition VS loop
Lodash partition VS native reduce (with Lodash actually loaded)
Comments
Confirm delete:
Do you really want to delete benchmark?