Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Filter vs _.uniq for unique
(version: 0)
Comparing performance of:
Set spread vs Array from set vs Filter vs _.uniq
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 40}, () => Math.floor(Math.random() * 140));
Tests:
Set spread
const f = [... new Set(array)]
Array from set
const s = new Set(array) const l = Array.from(s)
Filter
const b = array.filter((i,index) => array.indexOf(i)=== index)
_.uniq
const u = _.uniq(array)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Set spread
Array from set
Filter
_.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):
**Benchmark Explanation** The provided benchmark measures the performance of three different approaches to remove duplicates from an array: `Set` spread, `Array.from(set)`, and the `_.uniq()` function from the Lodash library. **Options Compared** 1. **Set spread**: This approach uses the spread operator (`...`) to create a new set from the input array. The set data structure automatically removes duplicates. 2. **Array from set**: This approach creates a new set from the input array, and then uses `Array.from()` to convert it back into an array. 3. **_.uniq()**: This is a function from the Lodash library that takes an array as input and returns a new array with duplicates removed. **Pros and Cons** 1. **Set spread**: * Pros: Efficient use of JavaScript's built-in set data structure, minimal overhead. * Cons: May not work correctly if the input array contains non-comparable values (e.g., strings vs numbers). 2. **Array from set**: * Pros: Works with any iterable, including arrays and sets. * Cons: Involves two function calls, which can be slower than a single operation. 3. **_.uniq()**: * Pros: Robust implementation that handles various edge cases, such as null or undefined values. * Cons: Requires an external library (Lodash), may incur additional overhead. **Library and Special JS Feature** The benchmark uses the `_.uniq()` function from Lodash, which is a popular utility library for JavaScript. The `_` symbol refers to the global scope in Node.js environments, where Lodash is often required. There are no special JavaScript features or syntaxes used in this benchmark. **Other Alternatives** Besides the three options mentioned above, other approaches to remove duplicates from an array include: 1. Using a `Map` data structure and iterating over its keys. 2. Using a regular expression with the `match()` method. 3. Implementing a custom algorithm using bitwise operations or other techniques. However, these alternatives may not be as efficient or convenient as the three options compared in this benchmark.
Related benchmarks:
Filter vs Set (get unique elements)
Filter vs Set (unique elements)
Diff size Set vs Filter for unique
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?