Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs map 4
(version: 0)
Comparing performance of:
Lodash uniqBy path vs Lodash uniqBy function vs Map vs Set and filter vs map and filter index
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var myArr = Array.from({ length: 16000 }, () => ({ value: Math.floor(Math.random() * 1000) })); var myCopy = null;
Tests:
Lodash uniqBy path
myCopy = _.uniqBy(myArr, 'value');
Lodash uniqBy function
myCopy = _.uniqBy(myArr, (obj) => obj.value);
Map
myCopy = [...new Map(myArr.map((obj) => [obj.value, obj])).values()];
Set and filter
const valuesSet = new Set(myArr.map(({ value }) => value)); myCopy = myArr.filter(({ value }) => valuesSet.has(value));
map and filter index
const valuesArr = myArr.map(({ value }) => value); myCopy = myArr.filter(({ value }, index) => valuesArr.indexOf(value) === index);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Lodash uniqBy path
Lodash uniqBy function
Map
Set and filter
map and filter index
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 definition and test cases. **Benchmark Definition** The benchmark is testing four different approaches to remove duplicates from an array of objects based on a specific property value: 1. **Lodash `uniqBy` method**: This function takes two arguments, the array to process and the function to extract the unique identifier from each object. 2. **Map-based approach**: This approach uses the `Map` data structure to store the unique values and then filters the original array based on these values. 3. **Set-based approach**: This approach uses a `Set` data structure to store the unique values and then filters the original array using this set. 4. **Map and filter index approach**: This approach maps each object in the array to its value, creates an array of indices that have seen each value, and then filters the original array based on these indices. **Lodash `uniqBy` method** The Lodash `uniqBy` function is used in two test cases: * `myCopy = _.uniqBy(myArr, 'value');` * `myCopy = _.uniqBy(myArr, (obj) => obj.value);` The first test case uses the string identifier `'value'`, while the second test case uses a custom function `(obj) => obj.value` to extract the unique identifier. **Map-based approach** This approach is used in one test case: * `myCopy = [...new Map(myArr.map((obj) => [obj.value, obj])).values()];` This approach creates a map where each key is a unique value and the corresponding value is the original object. The `values()` method is then called on this map to get an array of just the keys (unique values). This array is then used to filter the original array. **Set-based approach** This approach is used in one test case: * `const valuesSet = new Set(myArr.map(({ value }) => value));\r\nmyCopy = myArr.filter(({ value }) => valuesSet.has(value));` This approach creates a set of unique values and then filters the original array using this set. **Map and filter index approach** This approach is used in one test case: * `const valuesArr = myArr.map(({ value }) => value);\r\nmyCopy = myArr.filter(({ value }, index) => valuesArr.indexOf(value) === index);` This approach maps each object to its value, creates an array of indices that have seen each value using the `indexOf()` method, and then filters the original array based on these indices. **Pros and Cons** Here are some pros and cons for each approach: * **Lodash `uniqBy` method**: + Pros: Efficient, concise code. + Cons: Requires Lodash library. * **Map-based approach**: + Pros: Can be efficient if the number of unique values is small compared to the total array size. + Cons: Creates an additional data structure and may not perform well with very large arrays or many unique values. * **Set-based approach**: + Pros: Fast lookups, suitable for large arrays or many unique values. + Cons: May require more memory due to the set data structure. * **Map and filter index approach**: + Pros: Can be efficient if the number of unique values is small compared to the total array size. + Cons: Requires creating an additional array of indices, which can consume memory. **Other alternatives** Some other approaches that could be considered include: * Using a regular expression or string concatenation to remove duplicates * Using a sorting-based approach (e.g., sort the array and then take every second element) * Using a custom implementation with a data structure like a trie or a heap However, these alternative approaches may not be as efficient as the ones presented in the benchmark, especially for large datasets.
Related benchmarks:
Array.prototype.map vs Lodash.map on large data
Lodash uniqBy vs Map destructuring
native map vs lodash map on large array
lodash vs map 5
Comments
Confirm delete:
Do you really want to delete benchmark?