Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniqBy vs Reduce uniqBy
(version: 0)
Filtering array of objects by key
Comparing performance of:
Lodash vs Reduce
Created:
4 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 testFiles = _.range(_.random(100)).map(x => ({ id: _.random(1, 10), }));
Tests:
Lodash
_.uniqBy(testFiles,"id")
Reduce
testFiles.reduce((accumulator, item) => { if (item === null) { return accumulator; } const keyExist = accumulator.some((i) => i["id"] === item["id"]); return keyExist === true ? accumulator : [...accumulator, item]; }, []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/121.1 Mobile/15E148 Safari/605.1.15
Browser/OS:
Mobile Safari 17 on iOS 17.2.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
1051151.5 Ops/sec
Reduce
507165.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark created on MeasureThat.net. The benchmark compares the performance of two approaches for filtering an array of objects by a key: Lodash's `uniqBy` function and the built-in `reduce` method. **Options Compared** Two options are compared: 1. **Lodash's `uniqBy`**: This is a utility function from the Lodash library that takes an array of objects and a key as input, and returns a new array with duplicate entries removed based on the specified key. 2. **Built-in `reduce` method**: This is a built-in JavaScript method that applies a reduction function to each element in an array, reducing it to a single output value. **Pros and Cons** ### Lodash's `uniqBy` Pros: * Convenient and concise syntax * Handles edge cases, such as empty arrays or arrays with no duplicates * Well-tested and widely adopted Cons: * Adds overhead due to the library's inclusion in the benchmark code * May not be optimal for very large datasets ### Built-in `reduce` method Pros: * Native performance, eliminating the need for a library * Flexibility in customization through the reduction function * Can handle large datasets efficiently Cons: * Requires manual implementation of the reduction logic * May require additional setup or testing for edge cases **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks, such as array manipulation, object transformation, and more. In this benchmark, `uniqBy` is used to filter an array of objects by a key. **Test Case Analysis** The test cases compare the performance of each approach: 1. **Lodash's `uniqBy`**: The `uniqBy` function is called with the `testFiles` array and the `"id"` key as arguments. 2. **Built-in `reduce` method**: The `reduce` method is implemented using a custom reduction function that checks for duplicate keys in the accumulator. **Device-Specific Feature: iOS 17** The benchmark result shows a difference in performance between the two approaches on an iOS 17 device, with Lodash's `uniqBy` outperforming the built-in `reduce` method. This suggests that there may be some underlying differences in how the JavaScript engine optimizes or executes these two functions on this specific platform. **Alternative Approaches** Other alternatives to compare in this benchmark could include: * Using a different filtering library, such as Moment.js or Fast.js * Implementing the filtering logic using other JavaScript methods, like `filter` or `every` * Comparing the performance of Lodash's `uniqBy` with the built-in `reduce` method on different devices (e.g., desktop vs. mobile)
Related benchmarks:
reduce-vs-uniqBy
uniq-vs-uniqWith
bigArray uniq vs set vs reduce
bigArray uniq vs set vs reduce few duplicate
Comments
Confirm delete:
Do you really want to delete benchmark?