Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set to array
(version: 0)
Comparing performance of:
Set vs Array
Created:
6 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 compares two methods for removing duplicate values from an array: using the `Set` object in JavaScript and using the `uniq` function from the Lodash library. Here's a breakdown: **Options Compared:** * **`Set` Approach:** This method leverages the built-in `Set` object in JavaScript. Sets inherently store only unique values, so we create a Set from our array of numbers, and then convert it back to an array. * **Lodash `uniq` Function:** The Lodash library is a popular collection of utility functions for JavaScript. The `_.uniq()` function specifically takes an array as input and returns a new array containing only the unique values from the original array. **Pros and Cons:** * **`Set` Approach:** * **Pros:** * **Potentially faster:** Sets are optimized for uniqueness, so this method could be faster in some cases. * **Native JavaScript:** No external library dependency. * **Cons:** * **Slightly more verbose:** The code requires a few extra steps compared to Lodash's concise solution. * **Lodash `uniq` Function:** * **Pros:** * **Concise and readable:** The function provides a clear, single-line solution. * **Well-tested and optimized:** Lodash functions are rigorously tested and often perform very efficiently. * **Cons:** * **Dependency on an external library:** You need to include the Lodash library in your project, which adds some overhead. **Other Considerations:** * **Performance Differences:** The benchmark results show that the `Set` approach is faster for this particular use case (removing duplicates from a numerical array). However, performance can vary depending on factors like array size, data types, and other code execution context. * **Use Case:** Choose the method that best suits your needs. If you prioritize concise code and are comfortable using Lodash, `_.uniq()` is a great option. If native JavaScript and potential performance gains are important, the `Set` approach might be preferable. **Alternatives:** While this benchmark focuses on two specific approaches, other methods exist for removing duplicates from arrays in JavaScript: * **Manual Filtering:** You can iterate through the array using a `for` loop and manually check if each element is already present in a new array to avoid duplicates. This approach is less efficient than the `Set` or Lodash solutions.
Related benchmarks:
lodash uniq vs native uniq
uniqBy vs stringify performance
Lodash - uniq
Lodash uniqBy vs Javascript uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?