Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash UniqueWith vs custom filter for de-duplication
(version: 0)
Comparing performance of:
_.uniqWith vs filter vs Set
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].includes(item.b)) { return false; } trackObj[item.a] = [item.b]; return true; })
Set
const mySet = new Set(); myArray = myArray.filter(item => { const timestamp = item.a || ''; const value = item.b || ''; const key = timestamp.toString() + value.toString(); if (mySet.has(key)) { return false; } mySet.add(key); return true; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.uniqWith
filter
Set
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 its test cases. **What is tested?** The benchmark measures the performance of three different approaches for de-duplication in an array: 1. `_.uniqWith` from the Lodash library 2. A custom implementation using a filter function with a tracking object (`trackObj`) 3. Another custom implementation using a Set data structure **Options compared:** * The benchmark compares the performance of three different approaches: + `_.uniqWith` (Lodash library) + Custom implementation using a filter function and a tracking object + Custom implementation using a Set data structure * The options are compared in terms of execution speed, with the fastest approach declared as the winner. **Pros and cons:** 1. **_ _.uniqWith**: This approach uses the `_.uniqWith` function from Lodash, which takes two arguments: the array to be de-duplicated and a callback function that defines the de-duplication logic. * Pros: + Convenient and easy to use + Part of a well-established library (Lodash) * Cons: + Might incur overhead due to the additional dependency on Lodash 2. **Custom implementation using filter and tracking object**: This approach uses a filter function with a tracking object (`trackObj`) to keep track of items that have already been seen. * Pros: + No dependencies on external libraries + Can be more efficient than the Lodash version, as it avoids the overhead of a callback function * Cons: + Requires manual implementation and management of the tracking object 3. **Custom implementation using Set**: This approach uses a Set data structure to keep track of unique items. * Pros: + Fast and efficient, as Sets provide O(1) lookup and insertion operations * Cons: + Might require additional memory allocation for the Set **Library:** The `_.uniqWith` function from Lodash is a utility function that allows you to define custom de-duplication logic. The library provides a set of pre-defined functions (e.g., `_.isEqual`, `_.eq`) and also allows you to pass a custom callback function. **Special JS feature or syntax:** There are no special features or syntax used in this benchmark. The code is straightforward and focuses on the performance comparison of the three approaches. **Alternatives:** Other alternatives for de-duplication could include: * Using `Array.prototype.reduce()` with a callback function * Implementing a custom solution using a hash table or an object with keys * Utilizing other libraries like Moment.js or Underscore.js Note that these alternatives might not provide the same level of performance as the custom implementation using a Set, but they could offer convenience and ease of use.
Related benchmarks:
uniqBy vs stringify performance
get uniq values js
lodash uniqBy vs custom uniqBy
Lodash uniqBy vs Javascript uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?