Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter vs Set (get unique elements)
(version: 0)
Comparing performance of:
Filter vs Set
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({ length: 60 }, () => Math.floor(Math.random() * 140)); var uniqueF = function(arr) { arr.filter((a, b) => arr.indexOf(a) === b) } var uniqueS = function(arr) { [...new Set(arr)] }
Tests:
Filter
const filter = uniqueF(array)
Set
const filter = uniqueS(array)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Filter
2270660.0 Ops/sec
Set
1438537.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares two approaches to filter unique elements from an array: using `Array.prototype.filter()` (denoted as "Filter") versus using a Set data structure (`new Set()`) (denoted as "Set"). **Script Preparation Code** The script preparation code generates an array of 60 random integers between 0 and 139. It then defines two functions: * `uniqueF(arr)`: uses the `Array.prototype.filter()` method to filter out duplicate elements, keeping only the first occurrence of each element. * `uniqueS(arr)`: converts the input array to a Set data structure using `Array.from()`, which automatically removes duplicates. **Html Preparation Code** There is no HTML preparation code provided, so we'll assume this benchmark is running in a headless browser or Node.js environment. **Test Cases** The benchmark consists of two test cases: 1. "Filter": measures the performance of the `uniqueF()` function. 2. "Set": measures the performance of the `uniqueS()` function. **Library and Purpose** In both test cases, a library is used: `Array.prototype.filter()`. This method is a built-in part of the JavaScript Array prototype, which provides an efficient way to filter elements from an array while preserving the original order. The Set data structure (`new Set()`) is also used in the "Set" test case. A Set is a collection of unique values, and it provides fast lookups and removals. In this context, it's used to eliminate duplicates from the input array. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The code is straightforward and uses only built-in methods and data structures. **Pros and Cons of Different Approaches** * **Filter (Array.prototype.filter())**: Pros: + Efficient for small to medium-sized arrays + Preserves the original order of elements + Easy to implement Cons: + May be slower than Set-based approach for very large datasets + Can lead to unnecessary iterations if the filtering criteria is complex * **Set (new Set())**: Pros: + Efficient for large datasets, as it uses a hash table to store unique values + Fast lookups and removals + Eliminates duplicates automatically Cons: + May not preserve the original order of elements + Requires creating a new data structure **Other Alternatives** If you wanted to test alternative approaches for filtering unique elements, you might consider: * Using `Array.prototype.reduce()` or `Array.prototype.forEach()` * Implementing a custom iteration loop with conditional statements * Using other libraries like Lodash's `uniqBy()` function * Comparing the performance of different data structures, such as Arrays, Sets, or Maps Keep in mind that these alternatives might not be suitable for every use case, and their performance characteristics may vary depending on the specific requirements.
Related benchmarks:
Unique values of array
Filter vs Set (unique elements)
Filter vs Set (unique)
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?