Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash's uniq vs Array from Set
(version: 0)
Comparing performance of:
Set vs uniq
Created:
5 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 = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return Array.from(new Set(l));
uniq
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
uniq
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
8701142.0 Ops/sec
uniq
25772230.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript benchmark test case, where two different approaches to remove duplicates from an array are compared: using the `Array.from(new Set(l))` method and Lodash's `uniq()` function. **Test Cases** There are two individual test cases: 1. **Set**: This test case uses the `Array.from(new Set(l))` method to remove duplicates from the input array `[1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]`. 2. **uniq**: This test case uses Lodash's `uniq()` function to remove duplicates from the same input array `[1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]`. **Library: Set** The `Set` object in JavaScript is a collection of unique values. When used with the `Array.from()` method, it creates a new array containing only the unique elements from the original array. In this test case, the `Set` library is used to remove duplicates by converting the input array to a `Set`, which automatically removes any duplicate values. The resulting set is then converted back to an array using `Array.from()`, producing an array with no duplicates. **Library: Lodash's uniq** Lodash is a popular JavaScript utility library that provides various functions for tasks like string manipulation, array manipulation, and more. The `uniq()` function in Lodash removes duplicate values from an array. In this test case, the `uniq()` function is used to remove duplicates from the input array `[1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]`. The resulting array contains only unique values. **Comparison** The main difference between these two approaches is the way they handle duplicate elements: 1. **Set**: When using the `Array.from(new Set(l))` method, any duplicate elements are simply ignored, and the resulting array contains only unique values. 2. **uniq**: Lodash's `uniq()` function, on the other hand, includes the first occurrence of each element in the output array, discarding subsequent duplicates. **Pros and Cons** Here are some pros and cons of each approach: * **Set**: + Pros: Efficient, simple implementation, and always produces an array with unique values. + Cons: May not include duplicate elements, which might be desirable in certain situations. * **uniq**: + Pros: Includes the first occurrence of each element, making it suitable for use cases where duplicates need to be preserved or processed differently. + Cons: Less efficient than `Set` due to the extra processing required by Lodash's implementation. **Other Considerations** When choosing between these two approaches, consider the specific requirements of your use case: * If you need an array with only unique values and don't care about preserving duplicates, `Set` is likely a better choice. * If you need to preserve duplicate elements or process them differently, Lodash's `uniq()` function might be a better fit. **Alternatives** Other alternatives for removing duplicates from arrays in JavaScript include: 1. Using the spread operator (`[...new Set(arr)]`) with modern browsers and Node.js 2. Using a custom implementation using loops or recursive functions However, these alternatives may not offer the same level of performance, simplicity, or convenience as `Set` and Lodash's `uniq()` function.
Related benchmarks:
lodash uniq vs native uniq
uniqBy vs stringify performance
lodash uniq vs set - 3
Lodash - uniq
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?