Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Diff size Set vs Filter for unique
(version: 0)
Comparing performance of:
Set spread vs Array from set vs Filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 20}, () => Math.floor(Math.random() * 140));
Tests:
Set spread
const f = [... new Set(array)]
Array from set
const l = Array.from(new Set(array))
Filter
const b = array.filter((i,index,a) => a.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 break down the benchmark and explain what's being tested. **Overview** The benchmark is designed to compare three approaches for finding unique elements in an array: 1. `const f = [... new Set(array)]` (Set spread) 2. `\r\nconst l = Array.from(new Set(array))` (Array from set) 3. `const b = array.filter((i,index,a) => a.indexOf(i) === index)` (Filter) **What's being tested** The benchmark is testing the performance of these three approaches in finding unique elements in an array. The input array has 20 random integers between 0 and 139. **Options compared** * Set spread: Uses the spread operator to create a new set from the original array, which automatically removes duplicates. * Array from set: Creates a new array from the set created by `new Set(array)`, effectively removing duplicates. * Filter: Uses an array filter function with a custom condition to remove duplicates. **Pros and cons of each approach** 1. **Set spread**: * Pros: Simple, efficient, and widely supported in modern browsers. * Cons: May not be the most readable or maintainable code. 2. **Array from set**: * Pros: Also simple and efficient, but may have performance implications if the array is very large. * Cons: Requires an extra step to create a set before creating an array. 3. **Filter**: * Pros: Customizable and can be more readable for some developers. * Cons: May not be as efficient as the other two approaches, especially for large arrays. **Library usage** None of the benchmark tests explicitly use any libraries beyond the built-in JavaScript `Set` object and array methods. **Special JS feature or syntax** The test cases do not use any special JavaScript features or syntax beyond what's required to demonstrate the three approaches. However, it's worth noting that some browsers may support additional features like `const f = [... new Set(array)].map((x) => x);` which uses `map()` function along with set spread but this is not explicitly tested in this benchmark. **Other alternatives** If you want to test other approaches for finding unique elements, here are a few examples: * Using a `for...of` loop and pushing unique elements into an array. * Using a custom recursive function to find unique elements. * Using a data structure like a Map or a Set with custom keys. However, these alternatives may not be as straightforward or efficient as the approaches tested in this benchmark.
Related benchmarks:
Unique values of array
Set vs Filter for unique 40k
Filter vs Set (unique elements)
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?