Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash partition VS native reduce
(version: 0)
Comparing performance of:
Lodash vs Native reduce
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 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:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
240499.2 Ops/sec
Native reduce
3738.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided JSON represents two test cases for measuring the performance of partitioning an array in JavaScript. The first test case uses Lodash, a popular utility library for functional programming tasks, while the second test case uses native JavaScript methods. **Options Compared** In this benchmark, we're comparing the performance of: 1. **Lodash's `partition` method**: This function takes two arguments: the input array and a callback function that returns a boolean value indicating whether an element should be included in the first partition or not. 2. **Native JavaScript's `reduce` method**: This method applies a reduction function to each element of an array, accumulating a value across all elements. **Pros and Cons** ### Lodash's `partition` Pros: * Provides a convenient and readable way to separate elements into two partitions based on a simple condition. * Abstracts away the underlying implementation details, making it easier to write maintainable code. Cons: * Introduces an additional dependency (Lodash) that may impact performance in some cases. * Can be slower due to the overhead of calling a external function. ### Native JavaScript's `reduce` Pros: * Eliminates the need for an external library, reducing dependencies and potential performance impacts. * Can be faster since it doesn't involve calling a separate function. Cons: * Requires more code to achieve the same result, making it less readable for some developers. * May have performance issues if not implemented correctly (e.g., using `forEach` instead of `reduce`). **Library: Lodash** Lodash is a utility library that provides a wide range of functional programming helpers. The `partition` method is one of these helpers, allowing you to split an array into two partitions based on a condition. In this benchmark, Lodash's `partition` method is used to divide the input array into two parts: elements where the index is even and elements where it's odd. **Special JS Feature/Syntax** In this benchmark, we're using the **ES6 destructuring assignment** syntax (`var [met, rest] = ...;`) to extract the first element of the reduced array from the `reduce` method. This syntax was introduced in ECMAScript 2015 (ES6) and allows you to destructure arrays into separate variables. **Other Alternatives** If you're looking for alternative ways to partition an array, consider using: * **Array.prototype.filter()**: You can use `filter()` to create a new array with elements that pass a test. * **Array.prototype.forEach()**: While not as elegant as `reduce`, you can use `forEach()` in combination with `map()` or another function to achieve similar results. Keep in mind that these alternatives may have different performance characteristics and may require more code.
Related benchmarks:
Lodash reduce with native reduce
Lodash reduce vs native
Lodash reduce vs native in for loop
Lodash partition VS native reduce (with Lodash actually loaded)
Comments
Confirm delete:
Do you really want to delete benchmark?