Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs _.uniq
(version: 0)
Comparing performance of:
set vs uniq
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 = [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])]; return 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:
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 dive into explaining the provided benchmark. The goal of this benchmark is to compare the performance of two approaches: using `Set` in JavaScript and using Lodash's `uniq` function. The test case consists of creating an array with duplicate elements, filtering out duplicates, and returning the resulting array. **Options Compared:** 1. **Using `Set`**: This approach creates a new `Set` object from the array, which automatically removes duplicates due to its hash-based data structure. The resulting Set is then converted back to an array. 2. **Using Lodash's `uniq` function**: This approach uses the Lodash library to call the `uniq` function on the input array, which also removes duplicates. **Pros and Cons:** * Using `Set`: + Pros: - Efficient for large datasets, as Set operations are O(1) time complexity. - Simple implementation. + Cons: - Requires a library (Lodash) to be included in the HTML file, which may add overhead. - May require additional memory allocation due to the creation of a new Set object. * Using Lodash's `uniq` function: + Pros: - No additional library dependencies needed. - Can leverage existing optimizations in the Lodash implementation. + Cons: - May be slower than using `Set` for large datasets, due to the overhead of a JavaScript function call. **Library:** Lodash (version 4.17.10) is a popular JavaScript utility library that provides a wide range of functional programming helpers, including array utilities like `uniq`. **Special JS Features/Syntax:** This benchmark does not use any special JavaScript features or syntax beyond standard ECMAScript 2015 (ES6) features. **Other Alternatives:** If you're interested in exploring alternative approaches, here are a few options: * Using `reduce` with an initial value of an empty array and applying the callback function to each element. ```javascript var l = arr.reduce((acc, curr) => { if (acc.indexOf(curr) === -1) acc.push(curr); return acc; }, []); ``` * Using a `for...of` loop to iterate over the array elements and push unique values to an array. ```javascript var l = []; for (const elem of arr) { if (!l.includes(elem)) l.push(elem); } ``` Keep in mind that these alternatives may have different performance characteristics compared to using `Set` or Lodash's `uniq`.
Related benchmarks:
lodash uniq vs native uniq
Lodash - uniq
lodash vs ES6 uniq
Lodash uniqBy vs Javascript uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?