Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Filter for unique 40k
(version: 0)
Comparing performance of:
Set spread vs Array from set vs Filter
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 40000}, () => 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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set spread
Array from set
Filter
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 the explanation of the provided benchmark. **Benchmark Definition** The benchmark is designed to compare three different approaches for removing duplicates from an array: 1. Using the `Set` data structure with the spread operator (`[... new Set(array)]`) 2. Creating a `Set` and then converting it back to an array using `Array.from()` (`const s = new Set(array) \r\nconst l = Array.from(s)`) 3. Filtering out duplicates using the `filter()` method (`const b = array.filter((i,index) => array.indexOf(i)=== index)`) **Options Compared** The benchmark compares three different approaches for removing duplicates from an array: * **Set spread**: Uses the spread operator to create a new array with unique elements. * **Array from set**: Creates a `Set` and then converts it back to an array using `Array.from()`. * **Filter**: Filters out duplicates by checking if each element is present at its original index in the array. **Pros and Cons** 1. **Set spread**: * Pros: Simple, efficient, and easy to read. * Cons: May not be as efficient for very large arrays (due to the overhead of creating a new set). 2. **Array from set**: * Pros: Can handle very large arrays efficiently (since it only creates a single set data structure), but may be less readable due to the extra step. * Cons: Requires an additional operation (`Array.from()`) to convert back to an array, which can add overhead. 3. **Filter**: * Pros: Simple and easy to read, no extra operations required. * Cons: May not be as efficient for very large arrays (since it requires creating a new array with filtered elements). **Library and Purpose** In the benchmark definition, `Array.from()` is used to convert a set back to an array. This is a built-in JavaScript method that returns a new array from an iterable. **Special JS Feature or Syntax** None of the approaches in this benchmark utilize any special JavaScript features or syntax beyond standard ES6 syntax. **Other Alternatives** If you're looking for alternative ways to remove duplicates from an array, here are a few options: * Using `map()` and `includes()` methods: `array.map((i) => i.includes(i) ? array.indexOf(i) : false).filter(Boolean)` * Using `reduce()` method: `array.reduce((acc, curr) => (curr in acc) ? acc : [...acc, curr])` * Using `sort()` method followed by removal of duplicates: `array.sort().slice(0, -1)`
Related benchmarks:
Unique values of array
Filter vs Set (get unique elements)
Filter vs Set (unique elements)
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?