Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash "uniqWith", " "uniqBy", Node "Map"
(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) => val.toString());
Node Map
const unionUsers1 = Array.from(new Map(newUsers.map((user) => ([user.toString(), 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 is tested, compared options, pros/cons, and other considerations. **Benchmark Definition** The benchmark tests three JavaScript functions: `_.uniqWith` (from Lodash), `_.uniqBy`, and using a native Node.js `Map`. These functions aim to remove duplicate elements from an array. **Script Preparation Code** This code creates two arrays of user objects, `users` and `newUsers`, which are used as input for the benchmarking test cases. The script also generates 2000 new user objects with random properties and pushes them into the `newUsers` array. **Html Preparation Code** This line includes a script tag that loads Lodash version 4.17.10 from a CDN, making it available for use in the benchmark tests. **Individual Test Cases** There are three test cases: 1. **_.uniqWith** ```javascript const unionUsers1 = _.uniqWith(newUsers.concat(users), _.isEqual); ``` This test uses `_.uniqWith`, which removes duplicates based on a provided comparison function (`_.isEqual`). In this case, the comparison function is set to `_.isEqual`, which checks for object equality (i.e., by reference). Pros: * Can be useful when dealing with objects that have complex properties or relationships. * Allows for flexible comparison functions. Cons: * May not work well with primitive values or arrays, as it relies on object equality checks. * Can be slower than other approaches due to the overhead of comparing objects. 2. **_.uniqBy** ```javascript const unionUsers1 = _.uniqBy(newUsers.concat(users), (val) => val.toString()); ``` This test uses `_.uniqBy`, which removes duplicates based on a provided transformation function (`(val) => val.toString()`). This transformation function converts the values to strings, allowing for duplicate removal. Pros: * Fast and efficient, as it only requires string comparison. * Works well with primitive values and arrays. Cons: * May not work well with objects that have complex properties or relationships. * Can lead to slower performance if dealing with large amounts of data. 3. **Node Map** ```javascript const unionUsers1 = Array.from(new Map(newUsers.map((user) => ([user.toString(), user]))).values()); ``` This test uses a native Node.js `Map` to remove duplicates from the `newUsers` array. The `map` function creates an entry for each element in the array, using the string representation of the object as the key. The resulting map is then converted back into an array. Pros: * Fast and efficient, as it uses a native data structure optimized for lookup. * Works well with objects that have complex properties or relationships. Cons: * May not work well with primitive values or arrays. * Requires knowledge of Node.js `Map` API. **Other Considerations** The benchmark tests the performance of each function on identical input data, which helps to isolate the effects of each implementation. However, it's essential to note that this may not reflect real-world usage scenarios, where input data and use cases can vary significantly. Additionally, the benchmark results only provide a snapshot in time and do not account for other factors that might affect performance, such as: * Garbage collection overhead * Cache behavior of the JavaScript engine * Platform-specific optimizations In conclusion, the benchmark tests three different approaches to removing duplicates from an array. While each approach has its pros and cons, the Node.js `Map` approach is likely to be the fastest and most efficient for this specific use case.
Related benchmarks:
Lodash "uniqWith" "unionBy" "uniqBy" 6
Lodash "uniqWith" "unionBy" "uniqBy" 8
Lodash "uniqWith", " "uniqBy", Node "Map" 2
Lodash "uniqWith" "uniqBy" 7
Comments
Confirm delete:
Do you really want to delete benchmark?