Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set 1
(version: 0)
Comparing performance of:
Set vs Array
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>
Tests:
Set
var l = Array.from(new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])); return l;
Array
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Array
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of two different approaches to removing duplicate elements from an array: **1. Using `Set`:** This approach leverages JavaScript's built-in `Set` data structure, which inherently only stores unique values. We first create a `Set` containing our array of numbers, and then convert it back into an array. * **Pros:** * Concise and expressive code. * Sets are optimized for uniqueness checks, so this method is generally efficient. * **Cons:** * Might have slightly higher overhead due to the conversion between Set and Array. **2. Using Lodash's `uniq` function:** This approach utilizes the `uniq` function from the Lodash library, which specializes in removing duplicates from arrays. * **Pros:** * Lodash is well-optimized for common JavaScript tasks, so its `uniq` function could potentially be faster than a vanilla implementation. * Provides additional options (like preserving order or handling specific duplicate scenarios) that a basic `Set` approach might not offer. * **Cons:** * Adds an external dependency (Lodash), which can increase bundle size and introduce potential compatibility issues. **Other Considerations:** * **Array Size:** The performance difference between these methods is likely to be more pronounced with larger arrays. For very small arrays, the overhead of creating a `Set` might outweigh its efficiency. * **Use Case:** If your application frequently needs to remove duplicates from arrays, using Lodash's `uniq` might be more convenient due to its potential performance gains and additional features. However, if you prioritize minimal dependencies and simplicity, using `Set` is a great option. **Alternatives:** While this benchmark focuses on two specific approaches, other JavaScript libraries or techniques could also be explored for removing duplicates from arrays: * **Manual Filtering:** You can write your own custom function to iterate through the array and filter out duplicates. This offers flexibility but might require more code and testing. * **Reduce Function:** JavaScript's `reduce` method can be used in conjunction with a conditional statement to effectively remove duplicates. Let me know if you have any other questions!
Related benchmarks:
lodash uniq vs vanilla set - final
lodash uniq vs native uniq
uniqBy vs stringify performance
Lodash - uniq
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?