Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqWith vs uniqBy vs ES6 Set
(version: 0)
Comparing performance of:
uniqWith vs uniqBy vs ES6 Set
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
var users = [ { 'name': 'barney', 'age': 36 }, { 'name': 'pebbles', 'age': 5 } ]; var newUsers = [ { 'name': 'fred', 'age': 40 }, { 'name': 'barney', 'age': 36 }, { 'name': 'pebbles', 'age': 2 } ]; for(let i = 0; i < 2000; i++) { var newUser = { 'name': 'user_' + i, 'age': Math.floor(Math.random() * i) }; newUsers.push(newUser); } function uniques(arr) { const trackingSet = new Set(); return arr.map(function (obj) { if(!trackingSet.has(obj.name)) { trackingSet.add(obj.name) return obj; } }); }
Tests:
uniqWith
const unionUsers1 = _.uniqWith(newUsers.concat(users), _.isEqual);
uniqBy
const unionUsers2 = _.uniqBy(newUsers.concat(users), 'name');
ES6 Set
const unionUsers3 = uniques(newUsers.concat(users));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
uniqWith
uniqBy
ES6 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 what's being tested in the provided JSON. The test cases are designed to compare three approaches for removing duplicates from an array of objects: 1. **`_.uniqWith(newUsers.concat(users), _.isEqual)`**: This approach uses Lodash's `uniqWith` function, which takes a comparison function as its second argument. In this case, the comparison function is `_`, which means "identity" (i.e., it checks for equality using the object's own properties). The first step is to concatenate the `newUsers` and `users` arrays, then apply the `uniqWith` function to remove duplicates based on whether two objects are equal. 2. **`_.uniqBy(newUsers.concat(users), 'name')`**: This approach uses Lodash's `uniqBy` function, which takes a property key as its second argument. In this case, the property key is `'name'`, so it removes duplicates based solely on the `name` property of each object. 3. **`uniques(newUsers.concat(users))`**: This is a custom implementation using a Set, similar to Lodash's approach. It maps over the concatenated array, adds each object name to a Set (to track unique names), and then returns an array of objects with only the unique names. Now, let's discuss the pros and cons of each approach: **`_.uniqWith(_.isEqual)`**: Pros: Simple to implement, as it leverages Lodash's `uniqWith` function. It works well for this specific test case because the comparison is based on the object's own properties (using `_`). However, it may be slower than other approaches due to the overhead of the identity comparison. Cons: May not perform well with more complex objects or large datasets. **`_.uniqBy('name')`**: Pros: Fast and efficient for removing duplicates based solely on a single property (`'name'`). It's also relatively simple to implement, as it leverages Lodash's `uniqBy` function. Cons: May not work well if there are multiple duplicate properties (not just `'name'`), or if the objects have other properties that might affect uniqueness. **`uniques(newUsers.concat(users))`**: Pros: Can be faster than the Lodash approaches, as it avoids the overhead of a functional programming library. It's also easy to understand and implement. Cons: May not work well for more complex objects or large datasets, as it relies on a Set for tracking unique names. Additional considerations: * **Library choice**: The use of Lodash (specifically `uniqWith` and `uniqBy`) is an interesting choice, as it provides a convenient way to test these functions without having to implement them from scratch. * **Comparison function**: The comparison function used in `_.uniqWith` is `_`, which checks for equality using the object's own properties. This can be slow if there are many duplicate properties or complex objects. Other alternatives: * **Using a custom implementation**: Instead of relying on Lodash, you could implement your own version of `uniqBy` or use a different library (e.g., Ramda) that provides similar functionality. * **Testing with a different data set**: The test case assumes that the `name` property is sufficient for removing duplicates. You might want to test with other properties or more complex objects to better understand how each approach performs under different conditions. Overall, these tests provide a good starting point for comparing the performance of different approaches for removing duplicates from an array of objects.
Related benchmarks:
uniqWith vs uniqBy vs ES6 Set (isEqual)
Lodash "uniqWith", " "uniqBy", Node "Map"
Lodash "uniqWith", " "uniqBy", Node "Map" 2
Lodash "uniqWith" "uniqBy" 7
Comments
Confirm delete:
Do you really want to delete benchmark?