Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqWith vs uniqBy vs ES6 Set (isEqual)
(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 isEqual(val1, val2) { return val1.name === val2.name; } 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 dive into the benchmark and explore what's being tested. **Benchmark Definition** The benchmark is designed to measure the performance of three different approaches for removing duplicates from an array of objects: 1. `uniqWith`: This approach uses the `_.uniqWith` function from Lodash, which takes a custom equality function as an argument. In this case, the equality function is defined as `isEqual`, which checks if two objects have the same name. 2. `uniqBy`: This approach uses the `_.uniqBy` function from Lodash, which takes a property key as an argument to determine what should be used for comparison. 3. `ES6 Set`: This approach uses the `Set` data structure in JavaScript, which automatically removes duplicates. **Options Compared** The benchmark is comparing the performance of these three approaches on the same input data: * The original array of users (`users`) with a random subset of users added to it (`newUsers`) * The concatenation of `users` and `newUsers` **Pros and Cons of Each Approach** 1. **uniqWith**: This approach provides customizability, as the equality function can be tailored to specific use cases. However, this also means that the performance may vary depending on the complexity of the equality function. 2. **uniqBy**: This approach is faster than `uniqWith`, as it uses a simple property access instead of a custom equality function. However, it relies on the specific property being used for comparison, which might not always be desirable. 3. **ES6 Set**: This approach is likely to be the fastest, as sets are optimized for removing duplicates and have an average time complexity of O(1). However, it requires JavaScript to support sets natively. **Library and Its Purpose** The `lodash` library is used in this benchmark, specifically the `_uniqWith` and `_uniqBy` functions. These functions provide a convenient way to remove duplicates from arrays while preserving the original order or other properties of the objects. In this case, `_.uniqWith` takes a custom equality function (`isEqual`) as an argument, allowing for fine-grained control over what defines uniqueness. On the other hand, `_.uniqBy` uses the specified property key (`'name'`) to determine uniqueness. **Special JavaScript Feature or Syntax** None of the approaches in this benchmark rely on any special JavaScript features or syntax beyond standard JavaScript and Lodash functions. **Alternatives** Some alternative approaches for removing duplicates from arrays include: * Using `Array.prototype.filter()` with a callback function * Using `Array.prototype.map()` and then using `Set` to remove duplicates (not as efficient as using `Set` directly) * Using a custom implementation without relying on built-in functions like `Set` It's worth noting that the performance difference between these approaches may not be significant for small datasets, but can become more pronounced with larger inputs.
Related benchmarks:
uniqWith vs uniqBy vs ES6 Set
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?