Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "uniqWith", " "uniqBy", Node "Map" 2
(version: 0)
Comparing performance of:
uniqWith vs uniqBy vs Node Map
Created:
2 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); }
Tests:
uniqWith
const unionUsers1 = _.uniqWith(newUsers.concat(users), _.isEqual);
uniqBy
const unionUsers1 = _.uniqBy(newUsers.concat(users), (val) => JSON.stringify(val));
Node Map
const unionUsers1 = Array.from(new Map(newUsers.concat(users).map((user) => ([JSON.stringify(user), user]))).values())
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
uniqWith
uniqBy
Node Map
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's being tested. **Benchmark Overview** The benchmark measures the performance of three different approaches for removing duplicates from an array of objects: 1. `_.uniqWith` (Lodash utility function) 2. `_.uniqBy` (Lodash utility function) 3. Node's built-in `Map` data structure **Options Compared** Each approach is compared to its counterpart, making it a round-robin test. * `_.uniqWith` vs `_.uniqBy` * `_.uniqBy` vs Node's `Map` **Pros and Cons of Each Approach** ### _.uniqWith (Lodash) Pros: * Simple to use: takes an array and two functions as arguments. The first function checks for equality, while the second is a callback. * Fast: often faster than other approaches due to its optimized implementation. Cons: * Requires Lodash library * May not work well with complex data structures ### _.uniqBy (Lodash) Pros: * More flexible: allows using a custom comparison function (in this case, `JSON.stringify(val)`). * Works with more data types (arrays, objects). Cons: * Slower than `_.uniqWith` due to the additional function overhead. * May not be as efficient for very large datasets. ### Node's Map Pros: * Built-in, no extra library required * Optimized for performance: uses a hash table under the hood * Can handle large datasets efficiently Cons: * Requires creating a `Map` object and iterating over it to extract values. * May be less readable than other approaches. **Library Used** The benchmark uses Lodash (version 4.17.10) as a library, which provides two utility functions: `_.uniqWith` and `_.uniqBy`. **Special JS Feature or Syntax** None mentioned in the provided code snippets. However, it's worth noting that `JSON.stringify(val)` is used in both benchmark definitions to provide a custom comparison function for `_.uniqBy`. This ensures that objects are compared based on their contents (i.e., not by reference). **Other Alternatives** Some alternative approaches to removing duplicates from an array of objects include: * Using the `Set` data structure: similar to Node's `Map`, but with a different implementation. * Implementing a custom solution using recursion or iteration. * Using a library like `lodash-es` (a subset of Lodash without the `_.isEqual` function). Keep in mind that each approach has its own trade-offs and may be more or less suitable depending on your specific use case.
Related benchmarks:
Lodash "uniqWith" "unionBy" "uniqBy" 6
Lodash "uniqWith" "unionBy" "uniqBy" 27062023
Lodash "uniqWith" "unionBy" "uniqBy" 8
Lodash "uniqWith", " "uniqBy", Node "Map"
Comments
Confirm delete:
Do you really want to delete benchmark?