Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash UniqueWith vs custom filter to remove duplicates
(version: 0)
Comparing performance of:
_.uniqWith vs filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myArray = Array(7000).fill({a:1, b:2});
Tests:
_.uniqWith
myArray = _.uniqWith(myArray, _.isEqual);
filter
const trackObj = {}; myArray = myArray.filter(item => { if (trackObj[item.a] && trackObj[item.a] === item.b) { return false; } trackObj[item.a] = item.b; return true; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.uniqWith
filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares two approaches to remove duplicates from an array: 1. Using `lodash uniqWith` with a custom comparison function (`_.isEqual`) 2. Using a custom filter function with a tracker object **Lodash uniqWith** * Purpose: Remove duplicate elements from an array based on a custom comparison function. * Description: `uniqWith` takes two arguments: the array to process and a callback function that returns a boolean indicating whether two elements are equal. **Custom Filter Function** * Purpose: Implement a similar functionality as `lodash uniqWith` but without using the library. * Description: This function uses a tracker object (`trackObj`) to keep track of seen elements. It iterates through the array and checks if an element has been seen before (via `trackObj[item.a]`). If it has, it returns false, indicating that this is not the first occurrence of the element. Otherwise, it adds the element to the tracker object and returns true. **Comparison** The benchmark compares these two approaches: * **Performance**: Which one is faster? + Pros: Using `lodash uniqWith` is likely to be faster since it's a optimized library function. + Cons: The custom filter function may have performance overhead due to the manual iteration and comparison. * **Readability**: How easy is each approach to understand? + Pros: The custom filter function may be easier to understand for developers familiar with JavaScript basics, as it doesn't require knowledge of `lodash` or its API. + Cons: Using `lodash uniqWith` might be less intuitive without prior experience with the library. **Pros and Cons** * **Lodash uniqWith**: + Pros: Fast, well-tested, and widely adopted. + Cons: Requires external dependency (`lodash`), may not be familiar to developers without prior knowledge of the library. * **Custom Filter Function**: + Pros: Easier to understand for developers unfamiliar with `lodash`, no external dependencies. + Cons: May have performance overhead due to manual iteration and comparison, requires more code. **Other Alternatives** 1. Using a `Set` data structure to remove duplicates: ```javascript const seen = new Set(); myArray = myArray.filter(item => { if (seen.has(item.a)) return false; seen.add(item.a); return true; }); ``` This approach is concise and efficient but may not be as readable for developers unfamiliar with `Set` or JavaScript basics. 2. Using a library like `lodash` for more complex data structures, such as arrays of objects: ```javascript const _ = require('lodash'); myArray = _.uniqWith(myArray, _.isEqual); ``` This approach is fast and well-tested but may not be necessary for simple array duplicates removal. Keep in mind that these alternatives are not directly comparable to the custom filter function, as they have different trade-offs (e.g., readability vs. performance).
Related benchmarks:
unique elements in array using filter
uniqBy vs stringify performance
get uniq values js
Lodash uniqBy vs Javascript uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?